让我们继续从回溯文档的病人-医生类比。让我们假设医生模型是这样的:
{
"id": "4654654654654654654",
"name": "John Doe"
}
病人模型看上去基本相同(只有id和名称),两个模型与HasManyThrough关系相关联,与称为“预约”的通式模型相关联,就像在文档中一样。https://loopback.io/doc/en/lb2/HasManyThrough-relations.html
我的问题是,当您到达/api/ patient url时,您如何才能查询响应,以便包括每个医生的预约日期和病人?
想要的产出:
{
"id": "4654654654654654654",
"name": "John Doe",
"patients": [
{ "id": "1321232313", "name": "First Patient", "appointment_date": 1995-12-17T03:24:00 },
{ "id": "1321232313", "name": "Second Patient", "appointment_date": 1995-12-17T03:24:00 }
]
}
字段"appointment_date“是从约会到模型的日期,病人的名字是通过嵌套的包括同一模型的方式获得的。
有什么想法吗?
发布于 2018-01-23 21:53:21
使用嵌套包含,只需检查链接及其帮助。
请查阅本守则:
physicians.find(
{
include: {
"relation": "Appointment",
"scope": {
"fields": ["id", "patientId", "AppointmentDate", "physiciansId"], /*need to include both Person and PersonID fields for this to work*/
"include": {
"relation": "patient",
"scope": {
"fields": {"id": true, "patienName": true},
where: {id: patientId},
}
}
}
}
}
https://stackoverflow.com/questions/48391685
复制