sequelize中如何获取多个关联结果

How to get multiple association results in sequelize

我在获取关联结果时遇到问题。

我有两个模型 1 - 用户 2 - 项目

User.hasMany(项目, { foreignKey : 'user_Id' }); Project.belongsTo(用户, { foreignKey: { 名称: 'user_Id', 类型: DataTypes.UUID } })

当我通过项目模型获取数据时,相应的用户信息也会被获取。

    getAllProjects: async (req, res) => {
        const projects = await Project.findAll({ include: User});
        res.json({projects})
    }

以上代码的结果

results

{
    "projects": [
        {
            "id": "9bb3b99c-ac14-48fa-a0f2-e707f1d517ad",
            "name": "this is project name",
            "budget": "",
            "details": null,
            "days": null,
            "deadline": null,
            "user_Id": "02ef60af-0b1e-45e9-b393-55e124673143",
            "start_date": null,
            "status": 0,
            "createdAt": "2022-03-23T03:02:56.000Z",
            "updatedAt": "2022-03-23T03:02:56.000Z",
            "User": {
                "id": "02ef60af-0b1e-45e9-b393-55e124673143",
                "name": null,
                "email": null,
                "password": null,
                "userType": "client",
                "createdAt": "2022-03-23T03:02:35.000Z",
                "updatedAt": "2022-03-23T03:02:35.000Z"
            }
        }
    ]
}

但是相反,当我尝试获取用户及其项目时,获取了用户信息,但未获取相关项目。

代码

getAllUsers: async (req, res) => {
    const users = await User.findAll({inlcude: [{model: Project}]})
    res.json({users})
}

** 上述代码的结果**

{
    "users": [
        {
            "id": "02ef60af-0b1e-45e9-b393-55e124673143",
            "name": null,
            "email": null,
            "password": null,
            "userType": "client",
            "createdAt": "2022-03-23T03:02:35.000Z",
            "updatedAt": "2022-03-23T03:02:35.000Z"
        }
    ]
}

请帮助我。提前致谢。

只需更正从 inlcudeinclude 的拼写错误。

const users = await User.findAll({include: [{model: Project}]})