这个Node Javascript单行条件有什么问题?

时间:2015-05-06 08:06:19

标签: javascript node.js express

这是:

( result.username === user.username ) ? res.status( 500 ).json( "That username is already taken." ) : res.status( 500 ).json( "That email has already been used." )

如果条件为真,那么第一件事res.status( 500 ).json( "That username is already taken." )不应这样做吗?相反,它说:

[TypeError: undefined is not a function]

这可以按预期工作。

if ( result.username === user.username ) return res.status( 500 ).json( "That username is already taken." )
else return res.status( 500 ).json( "That email has already been used." )

对不起,如果我还没有发现错字。

2 个答案:

答案 0 :(得分:0)

以下代码可以:

var express = require('express');
var app = express();

app.get('/', function (req, res) {   
    (true) ? res.status( 500 ).json( "That username is already taken.") : res.status( 500 ).json( "That email has already been used." )
});

var server = app.listen(9000, function () {

var host = server.address().address;
var port = server.address().port;

console.log('Example app listening at http://%s:%s', host, port);

});

答案 1 :(得分:-2)

json()中的字符串不是JSON格式, 尝试:

res.status( 500 ).json({msg:"That username is already taken."})