如何结合两个可观察物以创建新的可观察物?

时间:2020-03-04 08:57:17

标签: javascript angular ionic-framework rxjs ionic4

我有两个名为“ PatientsService”和“ AppointmentService”的服务。在第三项服务“ AppointedPatientsService”中,我想订阅AppointmentService以获取所有带有PatientId的预定约会,然后我要重复订阅“ PatientsService.getPatient(PatientId)以获取带有PatientId的Patient数据。然后,我想返回一个名为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;
}))
);

}

这不起作用,我知道必须有更好的方法来处理这种情况。请帮助...

2 个答案:

答案 0 :(得分:2)

您通过尝试同步返回allAppointedPatients数组,将异步代码(可观察到的内容)与同步代码弄混了。

首先了解异步代码在Javascript中的工作方式,以及为什么Observable(流)如此有用。

尝试下面的代码,并确保您理解。当然,我无法对其进行测试,因此可以根据需要进行自己的更改。

getAppointments(): Observable<AppointedPatient[]> {
    return this.appointmentService.fetchAllAppointments()
        .pipe(
            switchMap(appointments => {

                const pacientAppointments = [];

                for (const appointment of allAppointments) {

                    // Extract the data aggregation outside or create custom operator
                    const pacientApp$ = this.patientsService.getPatient(appointment.patientId)
                        .pipe(
                            switchMap((pacient) => of(
                                new AppointedPatient(
                                    patient.firstName,
                                    patient.lastName,
                                    patient.address,
                                    patient.casePaperNumber,
                                    appointment.appointmentDateTime
                                )
                            ))
                        )

                    pacientAppoinments.push(pacientApp$);
                }

                return forkJoin(pacientAppointments);
        });
}

答案 1 :(得分:1)

您可以使用forkJoin:

forkJoin(
            getSingleValueObservable(),
            getDelayedValueObservable()
            // getMultiValueObservable(), forkJoin on works for observables that complete
          ).pipe(
            map(([first, second]) => {
              // forkJoin returns an array of values, here we map those values to an object
              return { first, second };
            })
          );