首页 > 解决方案 > sequelize query findAll using multiple constraints in where arg

问题描述

New to sequelize here!

looking for a way to use 2 arguments from res.query.

without any request.query works OK.

hardcoded all works OK.

with only phone also works OK.

Any way to use both of them at same time (doing AND or OR) between them?

or.... any way to check all var in res.query and apply them as required!?

regards

exports.findAll = (req, res, next) => {
  const { phone, active } = req.query;  // phone(string), active(boolean)

  let condPhone = phone ? { userPhone: { [Op.like]: `%${phone}%` } } : null;
  let condActive = active ? { userActive: Boolean(active) } : null;

  Saloia.Users.findAll({ where: condActive })
    .then((data) => res.send(data))
    .catch((err) => {
      err.status = 500;
      next(err);
    });
};

标签: javascriptexpresssequelize.js

解决方案


You can put multiple conditions as an object for the value of where.

Saloia.Users.findAll({
  where: {
      foo: bar,
      foo2: bar2
    }
});

推荐阅读