嗨,我有一个 User
模型和一个 ProfilePicture
模型。一个用户只能拥有一张头像,一张头像只能属于一个用户。
我的问题是在我的获取请求中,我正在调用个人资料图片模型并包括用户模型,我只得到我的个人资料图片而没有我的用户对象。
User.hasOne(pp,
{ foreignKey: "userId" }
);
pp.belongsTo(User,
{ foreignKey: "userId" }
);
router.get(
"/pp",
auth,
async (req, res) => {
let { id } = req.user;
await pp.findOne({
where: {
userId: id
}
},
{
include: [{
model: User,
}]
}).then((user) => {
console.log(user)
if (!user)
return res.status(404).send();
res.send(user);
});
});
最佳答案
您需要在第一个参数中包含 include
。
await pp.findOne({
where: {
userId: id
},
include: [
{
model: User
}
]
});
关于javascript - Sequelize include 不适用于 hasOne 关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66514363/