javascript - 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);
});
};
解决方案
You can put multiple conditions as an object for the value of where
.
Saloia.Users.findAll({
where: {
foo: bar,
foo2: bar2
}
});
推荐阅读
- reactjs - 单击按钮时效果函数无法呈现
- javascript - 我不能在函数上使用“this”
- node.js - 任何人都可以帮助我安装 React 吗?我收到了这个奇怪的错误
- amazon-web-services - 如何从本地连接到私有端点?
- python - OneHotEncoding:在一个数据帧中工作,而不是在非常非常相似的数据帧中工作(pyspark)
- html - 将地球仪和灯光元素对齐到树上,无论窗口的宽度如何
- java - 在java中实现树?没有硬编码
- c++ - 如何实例化 mbed Ble 类的 2 个对象?
- html - HTML 到 PDF:元素重叠固定页脚
- php - 使用 WooCommerce PDF 发票和装箱单通过 PHP 生成和获取发票 PDF