AgularFire合并2个流

时间:2020-03-08 14:51:49

标签: firebase rxjs angularfire2

我正在尝试使用AngularFire合并来自Firebase的2个数据流,但是我遇到了困难。

我有一个名为incomeShops的集合,该集合引用了创建该特定记录的userID。我需要做的是再次调用以获取用户数据,但我做不到。

有帮助吗?

这就是我所拥有的:


getAllIncomeShops() {
    const shops = this.venueService.getIncomeShops()
        .snapshotChanges()
        .pipe(
            switchMap(incomeShopSnaps => {
                // Mapping the result to extract the data I need using snapshotchanges().
                return incomeShopSnaps.map(incomeShopSnap => {
                    const shopObject = {
                        id: incomeShopSnap.payload.doc.id,
                        ...incomeShopSnap.payload.doc.data()
                    };
                    // On the shopObject there is a property called suggestedBy which contains a STRING with the user's ID.
                    // Trying to fetch the user's data to insert on the shopObject.
                    this.userService.getUser(shopObject.suggestedBy).valueChanges()
                });
            })
        ).subscribe(data => {
            console.log(data);
        });
}

1 个答案:

答案 0 :(得分:1)

您可以创建一系列可观察对象,并等到所有它们都以forkJoin()完成。

switchMap(incomeShopSnaps => {
  // Mapping the result to extract the data I need using snapshotchanges().
  const requests = incomeShopSnaps.map(incomeShopSnap => {
    const shopObject = {
      id: incomeShopSnap.payload.doc.id,
      ...incomeShopSnap.payload.doc.data()
    };
    // On the shopObject there is a property called suggestedBy which contains a STRING with the user's ID.
    // Trying to fetch the user's data to insert on the shopObject.
    return this.userService.getUser(shopObject.suggestedBy).valueChanges()
  });

  return forkJoin(requests); // You can eventually `map()` results into something...
}),