[英]How do I use both a '<>' and a 'whereNotIn' when querying with knex and Bookshelf?
I have a model, 'Excerpt', and want to fetch all excerpts that are NOT owned by a given user, and are NOT in a list of excluded excerpts (eg no excerpts with id's from the list [0, 1, 2, 3]) . 我有一个模型“摘录”,并且想要获取不属于给定用户的所有摘录,并且不在摘录清单中(例如,列表[0、1、2、3中没有ID的摘录) ]) 。
I've successfully selected all excerpts not owned by a user, by using: 我已经使用以下方法成功选择了用户不拥有的所有摘录:
Excerpt
.query({
whereNot: { owner_id : req.currentUser.id }
})
.fetchAll()
.then((excerptResults) => {
res.status(200).json(excerptResults);
});
and I have tried to use whereNotIn
to exclude excerpts with the following snippet (as per this stackoverflow post ) : 并且我尝试使用
whereNotIn
排除以下片段的摘录(根据此stackoverflow post ) :
Excerpt
.query({
whereNotIn: { id : [0, 1, 2, 3] }
})
.fetchAll()
.then((excerptResults) => {
var tasks = [];
for(var i=0; i<excerptResults.models.length; i++) {
tasks.push(excerptResults.models[i].attributes);
}
res.status(200).json(tasks);
});
Unfortunately, I get the following error message 不幸的是,我收到以下错误消息
Unhandled rejection Error: Undefined binding(s) detected when compiling SELECT query: select "excerpts".* from "excerpts" where "[object Object]" not in (?)
I don't really understand the error message. 我不太了解错误消息。 Does anybody have any ideas?
有人有什么想法吗?
It should for your case : 它应该为您的情况:
Excerpt.query(function(qb){
qb.where('id','not in', [0,1,2,3]).andWhere('owner_id','=',req.currentUser.id )
})
.fetchAll()
.then(...);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.