【发布时间】:2020-03-04 08:57:17
【问题描述】:
我有两个名为“PatientsService”和“AppointmentService”的服务。在第三个服务“AppointedPatientsService”中,我想订阅 AppointmentService 以获取所有带有 PatientId 的预约,之后我想重复订阅 PatientService.getPatient(patientId) 以获取带有 PatientId 的患者数据。然后,我想返回一个名为 allAppointedPatients 的新数组,它包含所有与患者数据的约会。我试过这个...
getAppointments() {
let allAppointments: Appointment[] = [];
const allAppointedPatients: AppointedPatient[] = [];
return this.appointmentService.fetchAllAppointments().pipe(
take(1),
tap(appointments => {
allAppointments = appointments;
for (const appointment of allAppointments) {
this.patientsService.getPatient(appointment.patientId).pipe(
tap(patient => {
const newAppointment = new AppointedPatient(patient.firstName,
patient.lastName,
patient.address,
patient.casePaperNumber,
appointment.appointmentDateTime);
allAppointedPatients.push(newAppointment);
})
).subscribe();
}
return allAppointedPatients;
}),
pipe(tap((data) => {
return this.allAppointedPatients;
}))
);
}
这不起作用,我知道必须有更好的方法来处理这种情况。请帮忙...
【问题讨论】:
-
你在哪里初始化你的数组?
-
寻找 rxjs 的 switchMap 操作符
-
@Ramesh - 在返回语句之前初始化数组.. 查看更新的代码快照
-
@enno.void - 我尝试使用开关映射。但我很困惑我应该在哪个可观察对象上使用 switchMap
标签: javascript angular ionic-framework rxjs ionic4