这个Sequelize查询哪里出错了?

编程入门 行业动态 更新时间:2024-10-05 23:21:26

这个Sequelize查询哪里出<a href=https://www.elefans.com/category/jswz/34/1725626.html style=错了?"/>

这个Sequelize查询哪里出错了?

我已经使用 sequelize 一段时间了,但这是我第一次遇到这样的错误。这是代码:

const results = await Article.findAll({
  where: {
    Sections: {
      [Op.any]: {
        contents: {
          ops: {
            [Op.any]: {
              insert: {
                [Op.like]: `%${searchString}%`
              }
            }
          }
        }
      }
    }
  }
});

这是我正在查询的数据结构的示例:

    "Sections": [
      {
        "contents": {
          "ops": [
            {
              "insert": "Test text\n"
            }
          ]
        },
        "type": "text",
        "id": 3,
        "createdAt": "2023-04-08T08:43:53.227Z",
        "updatedAt": "2023-04-22T09:32:00.133Z",
        "sectionId": 4,
        "articleId": 4
      },
      {
        "contents": {
          "ops": [
            {
              "insert": "Test text\n"
            }
          ]
        },
        "type": "text",
        "id": 5,
        "createdAt": "2023-04-08T09:42:30.266Z",
        "updatedAt": "2023-04-22T09:32:00.137Z",
        "sectionId": 4,
        "articleId": 4
      }
    ],

我试过转移一些操作,将所有东西分开在不同的对象中,但是,我不断收到错误:

Invalid value : {[Symbol(any)]: { contents: [Object] } }

根据我的试验和错误,我的理解是 sequelize 似乎无法读取超过一定级别的嵌套,因为它获得的值是

[Object]
而不是实际值?但怎么会这样呢?我应该如何解决这个问题?

编辑: 我今天挖得更深一点,发现了我的一个大错误。我假设 Sections 列是 article 表的一部分,但是,它只是一个外键,我完全忘记了它,这是我的责任。这首先解释了我面临的问题。 现在这是编辑后的代码:

Article.findAndCountAll({
      where: conditions,
      include: [{
        model: Section,
        as: 'Sections',
        where: {
          contents: {
            ops: {
              [Op.contains]: {
                insert: {
                  [Op.like]: `%${conditions.textSearch}%`
                }
              }
            }
          }
        }
      }],
      limit: pagination.limit,
      offset: pagination.page * pagination.limit || 0,
      order: [["createdAt", "DESC"]],
    })

但是,嘿,这也不能解决我的问题:我仍然得到

Error: Invalid value { insert: { [Symbol(like)]: '%test%' } }

这是“部分”结构:

const structure = {
  contents: DataTypes.JSONB,
  type: DataTypes.STRING,
  id: {
    type: DataTypes.INTEGER,
    autoIncrement: true,
    primaryKey: true
  }
};

和初始化:

  Article.hasMany(Section, { foreignKey: "sectionId" });
  Section.belongsTo(Article, { foreignKey: "articleId" });
回答如下:

更多推荐

这个Sequelize查询哪里出错了?

本文发布于:2024-05-30 06:10:57,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1770194.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:错了   Sequelize

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!