How to return result from include model in same level of main model in Sequelize?

Viewed 741

This is the code and result that I have done in my project. I would like to get the result of include model in the same result with the main model. the below code is what I have done.

Sequelize query:

 User.findAll({
        include: [{
                model: Position,
                attributes: ['POSITION_NAME'],
            }
        ]
    }).then(user => {
        res.json({
            data: user
        })
    }).catch(error => {
        console.log(error)
    })

result that I get

     [
        {
            "ID": 2,
            "NAME": "AAA",
            "LAST_NAME": "BBB",
            "Position": {
                "POSITION_NAME": "ວິຊາການສັນຍາຈ້າງ"
            }
        }
]

result that I want:

     [
        {
            
        "ID": 2,
        "NAME": "AAA",
        "LAST_NAME": "BBB",
        "POSITION_NAME": "ວິຊາການສັນຍາຈ້າງ"
        }
]
1 Answers

try like this .

User.findAll({
    attributes: ["ID", "NAME", ["Position.POSITION_NAME", "POSITION_NAME"]],
    include: [{
        model: Position,
        attributes: [],
    }
    ],
    raw:true
}).then(user => {
    res.json({
        data: user
    })
}).catch(error => {
    console.log(error)
})
Related