簡體   English   中英

Sequelize include 不適用於 hasOne 關系

[英]Sequelize include is not working with hasOne relationship

嗨,我有一個User model 和一個ProfilePicture model。 用戶只能擁有一張頭像,一張頭像只能屬於一個用戶。

我的問題是在我的獲取請求中,我正在調用個人資料圖片 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);
        });
    });

您需要在第一個參數中包含include

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM