使用mergeMap订阅可观察对象数组

时间:2019-08-16 14:58:44

标签: javascript angular typescript rxjs

我有一些我可以订阅/调用并获取数据的可观察对象和REST端点。

我想从可观察对象和REST端点获取所有数据,并使用它们创建一个变量以将其发送到REST端点

我正在使用forkJoinmergeMap,如下所示:

const sources: Array<Observable<any>> = [
      of(this.dataStore.trips$),
      of(this.dataStore.passengers$),
      of(this.dataStore.user$),
      of(this.dataStore.user$.pipe(
        mergeMap(user => <Observable<any>> this.userService.getUserIdByEmail(user.email))
      )),
      of(this.toursStore.totalInCart$),
      of(this.toursStore.totalInCart$.pipe(
        mergeMap(total => <Observable<any>> this.currencyService.getUSDConversionRate(total)),
      ))
    ];

forkJoin(sources).subscribe(
      observables => {
        const data = observables.map(observer => this.convertObservableToBehaviorSubject(observer, observer.source.value));

        console.log(data);

        booking = {
          booking: {
            created_at: Date.now(),
            trips: data[0].value,
            passengers: data[1].value,
            total: data[4].value,
            user_id: data[3].value,
            total: data[5].value,
          },
          user: data[2].value
        };

        console.log(booking);
      },
      err => console.log('Error:', err)
    );

将可观察对象转换为BehaviorSubject的方法:

convertObservableToBehaviorSubject<T>(observable: Observable<T>, initValue: T): BehaviorSubject<T> {
    const subject = new BehaviorSubject(initValue);

    observable.subscribe(
      (x: T) => {
        subject.next(x);
      },
      (err: any) => {
        subject.error(err);
      },
      () => {
        subject.complete();
      },
    );

    return subject;
  }

当我console.log(data)获得所有数据时,但是当我尝试将值分配给变量booking时,却没有为 user_id total

this.dataStore.user$.pipe(
   mergeMap(user => <Observable<any>> this.userService.getUserIdByEmail(user.email))
)

this.toursStore.totalInCart$.pipe(
        mergeMap(total => <Observable<any>> this.currencyService.getUSDConversionRate(total)),
      )

我希望user_idtotal拥有数据。

1 个答案:

答案 0 :(得分:0)

退后一步,您可以使它变得更简单

您不需要用of(...)包围可观察对象,因为它们首先已经是可观察对象。 of应该用于将对象传递给可观察对象。

只需将forkjoin与您已经拥有的可观察对象(例如字典)一起使用即可。 forkJoin({trips: this.datastore.trips$, passengers: this.datastore.passengers$, etc)

这意味着您将立即获得值,而不必将其转换为BehavourSubject

所以最终结果将是

forkJoin({trips: this.datastore.trips$, passengers: this.datastore.passengers$, etc...})
  .subscribe(data => {

        booking = {
          booking: {
            created_at: Date.now(),
            trips: data.trips,
            passengers: data.passengers,
            ...etc
          }
        };
  })