0

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

2 Answers 2

4

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}
Sign up to request clarification or add additional context in comments.

1 Comment

"where: Object.assign({}, condition , condition2)", Works! =) ty
0

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,
});

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.