6

我正在尝试在 SequelizeJS 中使用 $in 运算符,就像在 MySQL 语句中一样:

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

就我而言,我加入了三个表,但我认为这与问题无关。Error: Invalid value { '$in': ['12345','67890','15948'] }我使用以下代码不断收到此错误:

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

有人可以提供一些有关如何使用 $in 运算符的见解吗?我见过的唯一示例是在搜索 Id 时使用数组中的整数。

4

1 回答 1

14

只是在这里阅读文档Sequelize WHERE

我会尝试:

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
  }]
})

这有帮助吗?

于 2018-04-06T21:03:07.383 回答