I'm fairly new to Mongo DB/ Mongoose and want to be sure i'm approaching Mongoose errors the correct way. I'm trying to search for a document by its' _id. Here is my query:
const team = await Team.findOne({_id:req.body.invitedTeamID});
This works fine but I need to validate if any record was returned from this query so after a bit of research I amended it to be like so:
const team = await Team.findOne({_id:req.body.invitedTeamID}, function(err, doc){
if(doc.length === 0 || err){
console.log("no record found!")
}
});
When I enter a bogus object id for the invitedTeamID variable I get an ugly Mongoose rejected promise error saying something like:
CastError: Cast to ObjectId failed for value "005a99
This happens for either or of the above functions and I don't get my console.log statement.
Can someone please advise what's the correct way to handle this?
Thanks