简体   繁体   中英

What is wrong with this Node Javascript one-line conditional?

Here it is:

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

Shouldn't this do the first thing, res.status( 500 ).json( "That username is already taken." ) , if the condition is true? Instead, it says:

[TypeError: undefined is not a function]

This works as expected.

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." )

Sorry if I'm still not spotting the typo.

The following code will be ok:

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);

});

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM