Reputation: 39
How do i query a table with multiple conditions? This give me no error but, only run the first condition!
exports.findAllLimit = (req, res) => {
const titulo = req.query.titulo ;
var condition = titulo ? { titulo : { [Op.iLike]: `%${titulo }%` } } : null;
var condition2 = {stock: { [Op.ne]: 0}};
Produtos.findAll({
where: condition , condition2,
include: [Categorias],
order: [
['id', 'ASC']
],
limit: 9
})
.then(data => {
res.send(data);
})
.catch(err => {
res.status(500).send({
message:
err.message || "Ocorreu um erro a retirar os dados do backend!."
});
});
};
Upvotes: 0
Views: 3580
Reputation: 106
You create here an object with property condition2 and it's value. You need to merge these 2 conditions, and assign them on where. so you can use:
where: Object.assign({}, condition , condition2),
OR:
where: {...condition, ...condition2}
Upvotes: 4
Reputation: 2227
you can do like this for multiple condition .
const titulo = req.query.titulo ;
var condition = titulo
? {
titulo: {
[Op.iLike]: `%${titulo}%`,
},
}
: null;
var condition2 = {
stock: {
[Op.ne]: 0,
},
};
let where = [];
where.push(condition);
where.push(condition2);
Produtos.findAll({
where,
});
Upvotes: 0