Home > database >  How to find with a condition?
How to find with a condition?

Time:03-16

I'm trying to find data with:

     await User.find({$or: [{email: req.body.email}, {linkedEmail: req.body.email}]})
     .populate("roles")
     .exec((err, user) => {
         if (err) 
            res.status(500).send({message: err});
      });

The user always has an email registered and maybe could contain a linked email... So I'm trying to find it both ways but when I test it in postman it just loops in Sending request and not show a response

CodePudding user response:

Using the mongoose query builder can avoid some confusion. See doc

Also, since you're using async/await, you should use probably use try/catch instead of passing a callback to exec. cf doc

try {
  const user = await User.find()
    .or([{ email: req.body.email }, { linkedEmail: req.body.email }])
    .populate('roles')
  // ...
} catch (err) {
  res.status(500).send({ message: err })
}

  • Related