Angular 在 Map 中添加 Observable 结果

Angular add Observable result inside Map

我目前在我的服务中有一个方法可以从 Firebase (Firestore) 检索集合并将其return作为可观察对象。

getInstallers(): Observable<any[]> {
    const installersCollection = this.afs.collection<Installer>('installers');
    return installersCollection.snapshotChanges().map(installers => {
      return installers.map(i => {
        const data = i.payload.doc.data() as Installer;
        const id = i.payload.doc.id;
        return { id, ...data };
      });
    });
  }

我有另一种方法,它利用 google 放置 api 和 return 一个地方的可观察值,当 place_id 传递给它时。我不打算包括所有内容,因为它有点长,只知道它 return 是 google 地点对象的可观察对象。

getPlaceDetails(placeId): Observable<any>

我想做的是这样的...

getInstallers(): Observable<any[]> {
    const installersCollection = this.afs.collection<Installer>('installers');
    return installersCollection.snapshotChanges().map(installers => {
      return installers.map(i => {
        const data = i.payload.doc.data() as Installer;
        const id = i.payload.doc.id;
        this.getPlaceDetails(data.placeId).subscribe(place => {
          return { id, ...data, ...place }
        });
      });
    });
  }

然而,这会导致该方法 return 无效。我正在尝试找出一种方法,将从我的 getPlaceDetails 可观察到的数据添加到从我的 firebase 集合映射的数据中。谢谢!

使用switchMap

getInstallers() : Observable<any[]> {
     const installersCollection = this.afs.collection<Installer>  ('installers');
     return installersCollection.snapshotChanges().switchMap(installers => {
        let arrayOfObservables = installers.map(i => {
         const data = i.payload.doc.data() as Installer;
         const id = i.payload.doc.id;
         return this.getPlaceDetails(data.placeId).map(place => {
            return { id, ...data, ...place }
         });
        });
        return Observable.forkJoin(arrayOfObservables);
     });
   }

在其他地方称它为:

 this.getInstallers().subscribe(data => {
   // here we have resukt
 })