node.js 在子数组上使用“where”和“in”时出现续集错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24920427/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sequelize error when using "where" and "in" on a subarray
提问by sports
This is my model definition:
这是我的模型定义:
var Tag = sequelize.define('Tag', {
name: Sequelize.STRING
});
var Event = sequelize.define('Event', {
name: Sequelize.STRING,
});
Event.hasMany(Tag, {as: 'tags', through: 'event_tags', foreignKey: 'eventId'});
Tag.hasMany(Event, {as: 'events', through: 'event_tags', foreignKey: 'tagId'});
In words this is: there are events and tags. Events are tagged with many tags.
用一句话来说就是:有事件和标签。事件被标记了许多标签。
I'm trying to run this query:
我正在尝试运行此查询:
Event
.findAndCountAll({
include: [{model: Tag, as: 'tags'}],
where: {'tags.id': {in: [1,2,3,4]}},
order: order,
limit: pageSize,
offset: pageSize * (page - 1),
})
.success(function(result) {
...
});
But I'm getting this error:
但我收到此错误:
\node_modules\sequelize\lib\dialects\abstract\query-generator.js:1134
var logicResult = Utils.getWhereLogic(logic, hash[key][logic]);
^
TypeError: Cannot read property 'in' of undefined
What am I doing wrong?
我究竟做错了什么?
I've used before the "in" expression, for example here:
我在“in”表达式之前使用过,例如这里:
Tag
.find({
where: {id: {in: [1,2,3,4]}}
}).success(...)
And it worked just fine.
它工作得很好。
I've never used the in expression with a sub-array though, as in this case. So I don't know if thats the correct way to do it.
不过,我从未在子数组中使用过 in 表达式,就像在这种情况下一样。所以我不知道这是否是正确的方法。
回答by Enxtur
No need "in" property, sequelize auto define this. Just set array.
不需要“in”属性,续集自动定义这个。只需设置数组。
Tag.findAll({
where: {
id: [1,2,3,4]
}
}).then(...)
回答by Brad
Updated example for Sequelize v5:
Sequelize v5 的更新示例:
await Tag.findAll({
where: {
id: {
[Sequelize.Op.in]: [1, 2, 3, 4]
}
}
});
回答by selmansamet
inproperty able to use at version 4.42
in可以在 4.42 版中使用的属性
Tag.findAll({
where: { id: {in: [1,2,3,4]} }
}).then(...)
And you can use notInproperty for excluded values.
您可以将notIn属性用于排除的值。
Tag.findAll({
where: { id: {notIn: [1,2,3,4]} }
}).then(...)
回答by jaddu nikhileswararao
replace in with $in
替换为 $in
where : { id : { $in : [1,2,3,4]}}

