【问题标题】:How do I use both a '<>' and a 'whereNotIn' when querying with knex and Bookshelf?使用 knex 和 Bookshelf 查询时,如何同时使用 '<>' 和 'whereNotIn'?
【发布时间】:2017-01-30 21:24:41
【问题描述】:

我有一个模型,“摘录”,并且想要获取所有不属于给定用户所有的摘录,并且不在排除摘录列表中(例如,列表中没有带有 id 的摘录 [0 , 1, 2, 3]).

我已经成功地选择了所有不属于用户的摘录,方法是:

Excerpt
    .query({
        whereNot: { owner_id : req.currentUser.id }
    })
    .fetchAll()
    .then((excerptResults) => {
      res.status(200).json(excerptResults);
    });

我尝试使用whereNotIn 排除带有以下sn-p 的摘录(根据this 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);
    });

很遗憾,我收到以下错误消息

Unhandled rejection Error: Undefined binding(s) detected when compiling SELECT query: select "excerpts".* from "excerpts" where "[object Object]" not in (?)

我不太明白错误信息。有人有什么想法吗?

【问题讨论】:

  • 试试Excerpt.whereNotIn('id', [0, 1, 2, 3])
  • @MukeshSharma 我刚刚收到错误消息Unhandled rejection TypeError: _excerpts2.default.whereNotIn is not a function

标签: javascript node.js knex.js bookshelf.js


【解决方案1】:

它应该适合你的情况:

Excerpt.query(function(qb){
   qb.where('id','not in', [0,1,2,3]).andWhere('owner_id','=',req.currentUser.id )
})
.fetchAll()
.then(...);

【讨论】:

  • 谢谢!这对我有用。我如何将它与我的原始 whereNot 查询链接起来以排除用户拥有的摘录?我不熟悉这种语法。
猜你喜欢
  • 2020-03-15
  • 1970-01-01
  • 1970-01-01
  • 2017-01-05
  • 2021-06-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多