AngularFire2 Firebase Observable永远不会结束(列表为空)

时间:2018-04-19 22:35:40

标签: firebase ionic-framework observable angularfire2

我试图查询空的火力列表。问题是可观察的方法订阅永远不会完成,我无法向用户显示ddbb列表为空。

函数getUserAppointmentsByDate(...)正在调用getUserAppointments(...),其中 this.database.list(' / appointment / users /' + user_uid)是输入用户(user_uid)的火焰列表。

如何管理对firebase的空查询?

提前感谢!

getUserAppointmentsByDate(user_uid: string, start: string, end: string) {

  if (typeof (user_uid) == "undefined" || typeof (start) == "undefined" || typeof (end) == "undefined") {
    console.error("invalid argument for getPatientReport");
    return;
  }

  return this.getUserAppointments(user_uid)
  .map(
    (appointment) => {
      return appointment
        .filter((appointment) => {
          var appointmentStart = new Date(appointment.start);
          var startFilter = new Date(start);
          var endFilter = new Date(end);
          //Filter old, not cancelled and not deleted
          return (appointmentStart.getTime() < endFilter.getTime())
            && (appointmentStart.getTime() > startFilter.getTime())
            && (appointment.status != AppointmentStatus.CANCELLED);
        });
    })
}

getUserAppointments(user_uid: string): any {
   return this.database.list('/appointment/users/' + user_uid) //*THIS IS AN EMPTY LIST
    .mergeMap((appointments) => {
      return Observable.forkJoin(appointments.map(
        (appointment) => this.database.object('/appointment/list/' + appointment.$key)
      .take(1)))
  })
}

1 个答案:

答案 0 :(得分:0)

this.database.list('/appointment/users/' + user_uid)返回一个空数组时。 Observable.forkJoin(appointments.map(完成而不发出任何值(这是forkJoin工作的预期方式)。在这种情况下,您有两个选项,在完整的功能中处理。

.subscribe(
  res => console.log('I got values'),
  err => console.log('I got errors'),
  // do it whatever you want here
  () => console.log('I complete with any values')
)

或处理if statement

import { of } from 'rxjs/observable/of';
...
return this.database.list('/appointment/users/' + user_uid)
    .mergeMap((appointments) => {
      if (appointments.length === 0) return of([]);
      return Observable.forkJoin(appointments.map(
        (appointment) => this.database.object('/appointment/list/' + appointment.$key)
      .take(1)))
  })