6

I'm using Mongoose:

user.find({username: 'xyz'}, function(err, doc){
  if(err){
    res.render('error', {errorMsg: "Error blah blah"})
  }
});

I'm deliberately using a user who doesn't exist xyz and it's not triggering any errors, I thought it was because of Mongoose but then I tried in MongoDB shell and yes MongoDB won't return an error if a record doesn't exist.

>db.accounts.find({username: 'xyz'})
> // no error, blank line

How do I handle that? I want the execution of the script stop if a user doesn't exist.

4
  • Possible dupe of stackoverflow.com/questions/12031041/… Commented Jun 9, 2016 at 16:12
  • Yea, that one is from 3 years ago. Things are constantly changing especially with NodeJS. Commented Jun 9, 2016 at 16:34
  • Sure, but this hasn't changed. A query that doesn't match anything just returns an empty result set (for find), or a null result (for findOne). Commented Jun 9, 2016 at 19:21
  • There may be new users, new views, new solutions to how this can be handled. Commented Jun 9, 2016 at 22:36

3 Answers 3

5

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
        }
    }
});
Sign up to request clarification or add additional context in comments.

1 Comment

Yea I would assume it would trigger an error. Thanks! I havent thought of checking if doc is empty!
1
var user = db.accounts.findOne({username: 'xyz'});

if (!user) {
   // handle error
   alert('fail');
}

2 Comments

Thanks but I can't think of a way how I can use this. I mean it would kind of work in Mongo shell (there is no alert in either shell or NodeJS) but there should be a callback that returns a result due to asynchronous nature of NodeJS. Even if I don't use Mongoose I'm not sure this is going to work unless it's some sort of a new hack I'm not aware of. Yet yes this is how you would do it in a syncronous environment.
@VeganSv If you're in an async function you can omit the callback and you will get a promise. Await it, then check its result.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.