Sequelize.js 拆下';包括';响应中的表名-Sequelize
你好,我有以下问题:Sequelize.js 拆下';包括';响应中的表名-Sequelize,sequelize.js,Sequelize.js,你好,我有以下问题: const availAll = await InstructorAvail.findAll({ raw: true, where: { [Op.or]: [ { date: sun, instructorId: instructorId }, { date: mon, instructorId: instructorId },
const availAll = await InstructorAvail.findAll({
raw: true,
where: {
[Op.or]: [
{ date: sun, instructorId: instructorId },
{ date: mon, instructorId: instructorId },
{ date: tues, instructorId: instructorId },
{ date: wed, instructorId: instructorId },
{ date: thurs, instructorId: instructorId },
{ date: fri, instructorId: instructorId },
{ date: sat, instructorId: instructorId },
],
},
include: [{ model: Timeslots, required: true }],
})
这将返回以下内容,我希望实现的是删除“时间段”:
{
"uuid": "cef9aff7-2b2a-4ac4-aa19-95003f5ee00c",
"date": "09May21",
"timeslot": "480",
"timeslotid": 29,
"status": 1,
"instructorId": 69,
"createdAt": "2021-05-10T20:46:45.000Z",
"updatedAt": "2021-05-10T20:46:45.000Z",
"Timeslots.id": 29,
"Timeslots.uuid": "0ff2019f-336c-439b-8876-fb4a44651556",
"Timeslots.time_integer": 480,
"Timeslots.name": "08:00 am",
"Timeslots.period": "morning",
"Timeslots.createdAt": "2021-05-10T18:52:37.000Z",
"Timeslots.updatedAt": "2021-05-10T18:52:37.000Z"
},
我想使用属性简化json,并从响应中删除“时间段”:
const availAll = await InstructorAvail.findAll({
raw: true,
where: {
[Op.or]: [
{ date: sun, instructorId: instructorId },
{ date: mon, instructorId: instructorId },
{ date: tues, instructorId: instructorId },
{ date: wed, instructorId: instructorId },
{ date: thurs, instructorId: instructorId },
{ date: fri, instructorId: instructorId },
{ date: sat, instructorId: instructorId },
],
},
attributes: [
'id',
'uuid',
'date',
'timeslot',
'status',
'instructorId',
'createdAt',
'updatedAt',
],
include: [{
model: Timeslots,
required: true,
attributes: [
['Timeslots.name', 'name'],
['Timeslots.period', 'period'],
]}],
})
有人知道这是否可能吗
提前感谢所以我最终通过修改代码解决了这个问题,如下所示:
const availAll = await InstructorAvail.findAll({
raw: true,
where: {
[Op.or]: [
{ date: sun, instructorId: instructorId },
{ date: mon, instructorId: instructorId },
{ date: tues, instructorId: instructorId },
{ date: wed, instructorId: instructorId },
{ date: thurs, instructorId: instructorId },
{ date: fri, instructorId: instructorId },
{ date: sat, instructorId: instructorId },
],
},
attributes: [
'id',
'uuid',
'date',
'timeslot',
'status',
'instructorId',
'Timeslots.name', //insert nested table.columnName here
'Timeslots.period',
],
include: [{
model: Timeslots,
required: true,
attributes: [] // return nothing from nested table here
}],
})
这为我提供了输出:
{
"id": 9127,
"uuid": "3d642c18-1e86-419c-8f6d-a6fba772e974",
"date": "27May21",
"timeslot": "870",
"timeslotId": 42,
"status": 1,
"instructorId": 69,
"name": "14:30 am",
"period": "afternoon"
},
您的示例代码将从响应中删除您不想要的大多数属性,如果您不想要
时隙中的任何属性,请使用属性:[]
谢谢您的回复,您的建议是解决方案的一部分。我刚刚添加了我需要的完整解决方案。