有了这个查询:
const noteRecords = await db.job_notes.findAll({
attributes: [],
include: [
{
model: db.jobs,
attributes: [],
where: {
id: jobId,
},
},
{
model: db.notes,
attributes: ['id', 'body'],
},
],
});
Sequelize 返回结果为:
[
{
"note": {
"id": "2",
"body": "Lorem Ipsume Dolor Sumut"
}
},
{
"note": {
"id": "3",
"body": "Sum es est sumus estis sunt"
}
}
]
虽然我希望它返回为:
[
{
"id": "2",
"body": "Lorem Ipsume Dolor Sumut"
},
{
"id": "3",
"body": "Sum es est sumus estis sunt"
}
]
除了
noteRecords = noteRecords.map(note => note.note)
之外,是否有一种 Sequelize 方法来以这种方式格式化数据?
最佳答案
通过将查询更改为:
const noteRecords = await db.notes.findAll({
include: [
{
model: db.jobs,
attributes: [],
where: {
id: jobId,
},
},
],
});
由于我在 db.notes 中的关联是:
models.notes.belongsToMany(models.jobs, {
through: 'job_notes',
foreignKey: 'note_id',
sourceKey: 'id',
});
关于node.js - Sequelize : Return associated results array without embedded objects,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56384079/