So i need to do a find by id or email. The input can be an objectId as a hex string or a valid email.
now a simple
User.find({'$or': [{email: req.body.user}, {_id: req.body.user}]}, function(err, user){})
is throwing an error when searching via email, since the email fails to return a valid objectId.
How do i get over this? I know one possible way is to first detect if the input is an email, and then query it like that, but any better method?
Actually, that’s the best method. In your application first detect what kind of input do you have. If it’s an
ObjectId– query by_id. If it’s email – query byemail.