最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to use the $in operator with SequelizeJS? - Stack Overflow

programmeradmin7浏览0评论

I'm trying to use the $in operator in SequelizeJS as you would in a MySQL statement:

SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1, value2, ...);

In my case, I've joined three tables, but I don't believe that's related to the issue. I keep getting this error Error: Invalid value { '$in': ['12345','67890','15948'] } with the following code:

definedTable.findAll({
  include: [{
    model: definedTableTwo,
    where: {
      zip_code: {
        $in: ['12345','67890','15948']
      }
    },
    required: true,
    plain: true
  },
  {
    model: definedTableThree,
    required: true,
    plain: true
  }]
})

Can someone provide some insight on how to use the $in operator? The only examples I've seen are with integers within the array when searching Ids.

I'm trying to use the $in operator in SequelizeJS as you would in a MySQL statement:

SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1, value2, ...);

In my case, I've joined three tables, but I don't believe that's related to the issue. I keep getting this error Error: Invalid value { '$in': ['12345','67890','15948'] } with the following code:

definedTable.findAll({
  include: [{
    model: definedTableTwo,
    where: {
      zip_code: {
        $in: ['12345','67890','15948']
      }
    },
    required: true,
    plain: true
  },
  {
    model: definedTableThree,
    required: true,
    plain: true
  }]
})

Can someone provide some insight on how to use the $in operator? The only examples I've seen are with integers within the array when searching Ids.

Share Improve this question asked Apr 6, 2018 at 20:33 SRecaSReca 6835 gold badges13 silver badges38 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 17

Just reading the docs here Sequelize WHERE

I'd try :

const Op = Sequelize.Op;
definedTable.findAll({
  include: [{
    model: definedTableTwo,
    where: {
      zip_code: {
        [Op.in]: ['12345','67890','15948']
          }
    },
    required: true,
    plain: true
  },
  {
    model: definedTableThree,
    required: true,
    plain: true
  }]
})

Does that help ?

发布评论

评论列表(0)

  1. 暂无评论