What is returned from Mongoose query that finds no matches?
If conditions were valid but no matches were found:
find
:err
isnull
,result
is[]
findOne
andfindById
:err
isnull
,result
isnull
However, if some condition was invalid (e.g. field is string
but you pass an object
, or you pass an invalid _id
)
For all three: err
is {..}
, result
is undefined
If using .find()
,
The handy way would be
models.<your collection name>.find({ _id: `your input` }).then(data => { if (data.length == 0) return // throw your error to the console;});