1

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.

Noah
  • 4,601
  • 9
  • 39
  • 52
  • 4
    Please post the exact error message, and where it is pointing at. – Bergi May 06 '15 at 08:08
  • Also, how does the line before this oneliner look like? – Bergi May 06 '15 at 08:08
  • Does it work if the condition is false? – Bergi May 06 '15 at 08:09
  • This really should either be an if/else statement or `res.status( 500 ).json( result.username === user.username ? "That username is already taken." ) : "That email has already been used." )` – Bergi May 06 '15 at 08:10
  • possible duplicate of [immediate shorthand if statement crashes the app](http://stackoverflow.com/q/27131613/1048572)? – Bergi May 06 '15 at 08:15

2 Answers2

0

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

});
yangyang
  • 1
  • 1
-2

The string in json() is not a JSON format, Try:

res.status( 500 ).json({msg:"That username is already taken."})
yangyang
  • 1
  • 1
  • You're not passing a JSON string either? – Bergi May 06 '15 at 08:25
  • Also, that hardly would throw a `[TypeError: undefined is not a function]` – Bergi May 06 '15 at 08:25
  • if ( result.username === user.username ) {res.status( 500 ).json( "That username is already taken." );} else {res.status( 500 ).json( "That email has already been used." );} Does this replicate the problem? – Anand May 06 '15 at 08:34
  • You are missing semicolon at the end of of first code example – Anand May 06 '15 at 08:39