Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sequelize.js “继续删除”;其中;如果查询不是';没有提供?_Sequelize.js - Fatal编程技术网

Sequelize.js “继续删除”;其中;如果查询不是';没有提供?

Sequelize.js “继续删除”;其中;如果查询不是';没有提供?,sequelize.js,Sequelize.js,所以我基本上是将一些查询字符串传递给我的端点用作过滤器,并使用Sequelize 我知道我可以使用where来过滤/查找记录,但我想这样做,如果我不提供查询字符串,它就会被忽略 当前: const { status } = req.query; const tickets = await models.Ticket.findAndCountAll({ where: { status, }, attributes: [ 'id', 'status' ],

所以我基本上是将一些查询字符串传递给我的端点用作过滤器,并使用Sequelize

我知道我可以使用
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);
可能重复: