gpt4 book ai didi

node.js - Sequelize : Return associated results array without embedded objects

转载 作者:行者123 更新时间:2023-12-03 22:37:44 24 4
gpt4 key购买 nike

有了这个查询:

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com