错了?"/>
这个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查询哪里出错了?
发布评论