Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/469.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Sequelize include与hasOne关系不起作用_Javascript_Node.js_Sequelize.js - Fatal编程技术网

Javascript Sequelize include与hasOne关系不起作用

Javascript Sequelize include与hasOne关系不起作用,javascript,node.js,sequelize.js,Javascript,Node.js,Sequelize.js,嗨,我有一个用户模型和一个档案图片模型。用户只能有一个配置文件图片,并且一个配置文件图片只能属于一个用户 我的问题是在我的get请求中,我调用了配置文件图片模型,包括用户模型,我只得到了我的配置文件图片,没有用户对象 User.hasOne(pp, { foreignKey: "userId" } ); pp.belongsTo(User, { foreignKey: "userId" } ); router.get( &quo

嗨,我有一个
用户
模型和一个
档案图片
模型。用户只能有一个配置文件图片,并且一个配置文件图片只能属于一个用户

我的问题是在我的get请求中,我调用了配置文件图片模型,包括用户模型,我只得到了我的配置文件图片,没有用户对象

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