Sequelize.js 当您不确定查询字符串是否存在时,在sequelize中处理查询字符串
请使用请求查询参数查询地址表。。 我想查询基于县,州和城市。 以致Sequelize.js 当您不确定查询字符串是否存在时,在sequelize中处理查询字符串,sequelize.js,Sequelize.js,请使用请求查询参数查询地址表。。 我想查询基于县,州和城市。 以致 Adressses.findall({ where: { country: req.query.country, state: req.query.state, city: req.query.city //what if user didnot select city? } 这个问题,我不确定
Adressses.findall({
where:
{
country: req.query.country,
state: req.query.state,
city: req.query.city //what if user didnot select city?
}
这个问题,我不确定用户会选择哪个查询参数。
如果undefined被通过怎么办?
如何避免属性的值为null或未定义。
或者我可以做以下事情:
Adressses.findall({
where:
{
country: req.query.country || "value that selects all",
state: req.query.state, || "or a fallback value",
city: req.query.city || "safe value",
}
我将非常感谢您的帮助。您可以在作为筛选器发送之前处理它。比如说
let filter={};
filter.test= req.query.test;
filter.id = req.query.id;
这里的测试是url和过滤器
localhost:3000/api/user?id=1
{
"id": "1"
}
localhost:3000/api/user?id=1&test=test
{
"test": "test",
"id": "1"
}
那么您的查询将被删除
Adressses.findall({
where: filter
})
是文档。