tedigc
tedigc

Reputation: 65

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 (e.g. no excerpts with id's from the list [0, 1, 2, 3]).

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):

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?

Upvotes: 0

Views: 1536

Answers (1)

Mukesh Sharma
Mukesh Sharma

Reputation: 9022

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(...);

Upvotes: 2

Related Questions