Skip to content
Advertisement

Node.js Sequelize: multiple condition on ‘where’

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!."
  });
   });
 };

Advertisement

Answer

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}
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement