node.js sequelize:多个“where”查询条件
我如何查询具有多个条件的表? 这里是两个工作的例子:
Post.findAll({ where: ['deletedAt IS NULL'] }).success()
和
Post.findAll({ where: {topicId: req.params.id} }).success()
那么,如果我需要结合条件,我觉得我需要做类似的事情
Post.findAll({ where: [{topicId: req.params.id}, 'deletedAt IS NULL'] }).success()
,但它不起作用。
什么是续集等待的语法?
节点debugging说:
DEBUG:TypeError:Object#没有方法'replace'
如果重要的话
请注意,截至本文和最新版本的sequelize,上述答案已过时。 我发布了解决scheme,希望能够节省一些时间。
filters["State"] = {$and: [filters["State"], {$not: this.filterSBM()}] };
注意JSON对象中的$和数组并缺less? 令牌replace。
或者采取更为一般的方式,因为这样可以帮助别人把头转过来:
{ $and: [{"Key1": "Value1"}, {"Key2": "Value2"}] }
你可以做:
Post.findAll({ where: {deletedAt: null, topicId: req.params.id} })
这将被转换为deletedAt IS NULL
顺便说一句,如果你需要deletedAt NOT IS NULL
,使用:
Post.findAll({ where: {deletedAt: {$ne: null}, topicId: req.params.id} })
做就是了:
Post.findAll( { where: ["topicId = ? AND deletedAt IS NULL", req.params.id] } ).success()
或者你需要一个或查询?
在新版本中试试这个
Post.findAll({ where: { authorId: 12, status: 'active' } }).then(function (data) { res.status(200).json(data) }) .catch(function (error) { res.status(500).json(error) });;