MongoDB find doesn't return an error if a record doesn't exist?
Well, if the "username" doesn't exist, that doesn't mean there is an error. Instead you should do something like this.
user.find({ username: 'xyz' }, function(err, doc){ if(doc.length === 0 || err){ res.render('error', { errorMsg: "Error blah blah" } ) }});
Or more verbose version:
user.find({ username: 'xyz' }, function(err, doc) { if(err){ res.render('error', { errorMsg: "Error blah blah" } ) } else { if (doc.length === 0) { console.log("User doesn't exist"); } else { //do something } }});
var user = db.accounts.findOne({username: 'xyz'});if (!user) { // handle error alert('fail');}
Try to check if the doc is null or not first and then you can get on with your function.
user.find({ username: 'xyz' }, function(err, doc){ if(doc === null || err){ res.render('error', { errorMsg: "Error blah blah" } ) } else { do something... }});