I have some issue using or statement is sequelize. Below is the code I have been able to e 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
I have some issue using or statement is sequelize. Below is the code I have been able to e 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
Share Improve this question asked Aug 29, 2017 at 17:47 dealwapdealwap 7213 gold badges16 silver badges38 bronze badges1 Answer
Reset to default 9According 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
}
]