首页 > 解决方案 > Feathers-Sequelize with Postgres:如何查询数组

问题描述

我想看看一个数组中是否包含一个或多个值。

这是一个模型字段定义:

 keywords: {
              type: Sequelize.ARRAY(Sequelize.STRING),
              defaultValue: [],
              allowNull: false,
           },

这是我正在测试的查询:

context.app.services.jobOffers.Model.findAll({
        where: {
            keywords: {
                $contains: ['hello'],
            },
        },
    })
     .then(result => {
         console.log('RESULT:', result)
     })
     .catch(err => {
            console.log('ERROR:', err)
     })

这是我收到的错误:

ERROR: TypeError: values.map is not a function

这是对类似问题的引用,我试图复制解决方案但没有效果。

https://github.com/feathersjs-ecosystem/feathers-sequelize/issues/135

标签: postgresqlsequelize.jsfeathersjsfeathers-sequelize

解决方案


似乎问题出在 Sequelize 版本上。Feathers.js 的一位作者@daffl 向我阐明了这一点

“在 Sequelize v5 及更高版本中,您需要使用符号,如[Op.contains]参见 ( http://docs.sequelizejs.com/manual/querying.html#operators )”

所以解决方法是更改​​这行代码

where: {
         keywords: {
          [Op.contains]: ['hello'],
         },
       },

推荐阅读