Sequelize.js “继续删除”;其中;如果查询不是';没有提供?
所以我基本上是将一些查询字符串传递给我的端点用作过滤器,并使用Sequelize 我知道我可以使用Sequelize.js “继续删除”;其中;如果查询不是';没有提供?,sequelize.js,Sequelize.js,所以我基本上是将一些查询字符串传递给我的端点用作过滤器,并使用Sequelize 我知道我可以使用where来过滤/查找记录,但我想这样做,如果我不提供查询字符串,它就会被忽略 当前: const { status } = req.query; const tickets = await models.Ticket.findAndCountAll({ where: { status, }, attributes: [ 'id', 'status' ],
where
来过滤/查找记录,但我想这样做,如果我不提供查询字符串,它就会被忽略
当前:
const { status } = req.query;
const tickets = await models.Ticket.findAndCountAll({
where: {
status,
},
attributes: [
'id',
'status'
],
});
如果我没有将状态作为查询字符串传递,它将失败,因为其中
无法执行。我也试着将未定义的传递给它,但没有运气,它错误地说;
错误:其中参数“status”具有无效的“undefined”值
总之,我想这样做,如果我确实提供了查询字符串,它们将被插入到where
子句中,但是如果它们不是,那么它就被忽略了。这只是一个只包括where
选项的情况,如果您知道status
具有Sequelize可以使用的值
const { status } = req.query;
// declare our query options
const options = {
where: {},
attributes: [
'id',
'status'
]
};
// if status has a value (!== undefined), include it in the query
if (status !== undefined)
options.where.status = status;
const tickets = await models.Ticket.findAndCountAll(options);
可能重复: