1

I have some issue using or statement is sequelize. Below is the code I have been able to come up with:

UserExist(req, res) {
const field = req.body.username || req.body.email;
const getFieldName = field === req.body.username ? 'username' : 'email';
return User
  .findOne({ where: {
    $or: [
      {
        email: req.body.email,
        username: req.body.username
      }
    ]
  }
  })
  .then((user) => {
    if (user) {
      res.status(200).send({ message: `${getFieldName} already exist` });
    }
  })
  .catch(() => res.status(404).send({ message: '' }));

}

Instead of executing the OR statment it only selects * from Users

1 Answer 1

9

According to the sequelize documentation

you should give different where conditions as different element of $or array

Please try this:

$or: [
  {
    email: req.body.email
  },
  {
    username: req.body.username
  }
]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.