Sequelize error when using "where" and "in" on a subarray

node.jssequelize.js

node.js Problem Overview


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:

 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.

node.js Solutions


Solution 1 - node.js

No need "in" property, sequelize auto define this. Just set array.

Tag.findAll({
    where: {
        id: [1,2,3,4]
    }
}).then(...)

Solution 2 - node.js

Updated example for Sequelize v5:

await Tag.findAll({
  where: {
    id: {
      [Sequelize.Op.in]: [1, 2, 3, 4]
    }
  }
});

Solution 3 - node.js

in property able to use at version 4.42

Tag.findAll({
    where: { id: {in: [1,2,3,4]} }
}).then(...)

And you can use notIn property for excluded values.

Tag.findAll({
    where: { id: {notIn: [1,2,3,4]} }
}).then(...)

http://docs.sequelizejs.com/manual/querying.html#operators

Solution 4 - node.js

I know this is hella old. But I'm pretty sure what you were looking for is as follows. At least one of these should do the trick. Probably both. I can't test at the moment because I'm in the middle of a 43-files-changed spaghetti code beast. I'll update with an actual test when I find the time.

(I'm posting now because all the rest seem to not actually answer the question asked, but they did answer the question I had when I arrived here.)

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

    ...
});
Event
.findAndCountAll({
    include: [
      {
        model: Tag,
        as: 'tags',
        where: { id: {in: [1,2,3,4]} },
      }
    ],
    order: order,
    limit: pageSize,
    offset: pageSize * (page - 1),
})
.success(function(result) {

    ...
});

Solution 5 - node.js

replace in with $in

where : { id : { $in : [1,2,3,4]}}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionsportsView Question on Stackoverflow
Solution 1 - node.jsEnxturView Answer on Stackoverflow
Solution 2 - node.jsBradView Answer on Stackoverflow
Solution 3 - node.jssametView Answer on Stackoverflow
Solution 4 - node.jsRoboticRenaissanceView Answer on Stackoverflow
Solution 5 - node.jsjaddu nikhileswararaoView Answer on Stackoverflow