【发布时间】:2022-01-19 14:43:08
【问题描述】:
我正在使用以下关于 sequelize 的数据:
{
name: "Matti Luukkainen",
username: "mluukkai@iki.fi",
wish_read: [
{
id: 3,
url: "https://google.com",
title: "Clean React",
author: "Dan Abramov",
likes: 34,
year: null,
reading_lists: [
{
read: true,
id: 2
}
]
},
{
id: 4,
url: "https://google.com",
title: "Clean Code",
author: "Bob Martin",
likes: 5,
year: null,
reading_lists: [
{
read: false,
id: 2
}
]
}
]
}
我想使用命令where 过滤数据,只显示reading_lists.read = true 所在的博客。我的实现是下面的代码,但是过滤器不起作用。我想我不能直接在where 中传递一个对象,但我想不出另一种方式来做到这一点。
const user = await User.findByPk(req.params.id,{
attributes: { exclude: ['id', 'createdAt', 'updatedAt']},
include: [
{
model: Blog,
as: 'wish_read',
attributes: { exclude: ['userId']},
through: {
attributes: { exclude: ['user_id', 'blog_id', 'blogId', 'userId'] }
},
attributes: { exclude: ['id', 'createdAt', 'userId', 'updatedAt'] }
},
],
where: {
wish_read.reading_lists.read: true
}
})
【问题讨论】:
标签: node.js postgresql sequelize.js