简体   繁体   English

Sequelize include 不适用于 hasOne 关系

[英]Sequelize include is not working with hasOne relationship

Hi i have a User model and a ProfilePicture model.嗨,我有一个User model 和一个ProfilePicture model。 The user can only have 1 profile picture and a profile picture can only belong to one user.用户只能拥有一张头像,一张头像只能属于一个用户。

My problem is in my get request i am calling the profile picture model and including the users model, i get just my profile picture without my user object.我的问题是在我的获取请求中,我正在调用个人资料图片 model 并包括用户 model,我只得到我的个人资料图片而没有我的用户 object。

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);
        });
    });

You need to include include in the first parameter.您需要在第一个参数中包含include

await pp.findOne({
  where: {
    userId: id
  },
  include: [
    {
      model: User
    }
  ]
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM