mongodb - Mongoose - always return null instead of an error when no result found? -
why mongoose return null instead of error when no result found?
person.findone({ 'name': 'ghost' }, function (err, person) { console.log(err); // null if (err) return handleerror(err); });
the person 'ghost' not exist in db expecting error null instead. why? how can get error instead?
because mongoose return error when there error, not finding result not error. can handle response if got null, :
person.findone({ 'name': 'ghost' }, function (err, person) { if(person === null) { console.log('no results found'); } if (err) return handleerror(err); });
or
person.findone({ 'name': 'ghost' }, function (err, person) { if (err) { return handleerror(err); } else if (person) { return person; } else { return 'no results found'; } });
Comments
Post a Comment