sequelize中双重包含node.js

时间:2018-12-05 12:02:44

标签: node.js sequelize.js

我想在User.findAll函数中包含另一个模型。 SurveyResult模型属于模型调查。如何包含模型调查以显示调查结果属于SurveyResult

test.php

这是我回来的json:

async index (req, res) {
    try {
        const userData = await User.findAll({
            include: [ UserStatus, SurveyResult
            ]
        })
            .map(user => user.toJSON())

        res.send(userData)
    } catch (err) {
        console.log(err)
    }

}

我尝试过但是没用:

 {
    "id": 3,
    "email": "testing@gmail.com",
    "password": "$2a$08$Y22dOOIgyGhLAOokYluGxupKHRv8zRcbAVK1YEvWVUtoBl7dOsAYK",
    "name": "test",
    "forename": "test",
    "createdAt": "2018-12-05T11:25:30.000Z",
    "updatedAt": "2018-12-05T11:25:30.000Z",
    "AdminId": 1,
    "UserStatuses": [
        {
            "id": 3,
            "sendEmail": false,
            "sendResult": true,
            "createdAt": "2018-12-05T11:25:31.000Z",
            "updatedAt": "2018-12-05T11:25:31.000Z",
            "UserId": 3
        }

1 个答案:

答案 0 :(得分:1)

我认为您正在尝试将其包含在嵌套级别中,如果是这种情况,则可以通过这种方式完成

const userData = await User.findAll({
    include: [
        { model : UserStatus }
        { model : SurveyResult ,
            include: {
                model : Survey
            } 
        }
    ]
})

// OR ( Shorthand )

const userData = await User.findAll({
    include: [ UserStatus , { model : SurveyResult , include: [Survey] }]
})