AgularFire 结合 2 个流

AgularFire Combine 2 streams

我正在尝试使用 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);
        });
}

您可以创建一个可观察对象数组,然后等到所有对象都完成 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...
}),