从响应中删除 'included' Table 名称 - Sequelize
Remove 'included' Table Name from response - Sequelize
您好,我有以下问题:
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 }],
})
以下returns,我想实现的是去掉'Timeslots.':
{
"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"
},
您好,我有以下问题:
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 }],
})
以下returns,我想实现的是去掉'Timeslots.':
{
"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"
},