如何将Observable <observable <{......> []>解析为Observable <{...} []>?

时间:2019-03-26 18:10:30

标签: typescript rxjs observable

我试图将两个可观察对象合并为一个可观察对象,因此我可以使用来自这两个对象的信息。可观察对象都是来自NoSQL数据库(Cloud Firestore数据库)中两个不同集合的文档。两个集合共享一个具有相同值的字段(uid和partnerID)。我首先创建了一个查询以获取第一个可观察对象,该查询返回一个Observable<{Object1}[]>。知道我是否尝试添加代码以获取第二个可观察对象并将其与现有对象合并,我得到一个Observable<Observable<{Object1 merged with Object2}>[]>

如何确保所生成的可观察对象是由对象组成的数组,这些对象是我从数据库中提取的这两个对象的组合?

我的目标是在Angular项目中使用组合的可观察对象。我正在尝试使用rxjs运算符实现这一目标。

这是我的服务中没有第二个可观察对象的函数:

queryMatches(fieldNameOfRole, boolValueOfAccepted) {
    return this.authService.user$.pipe(
      switchMap(user => {
        return this.angularFirestore
          .collection('matches', ref => ref.where(fieldNameOfRole, '==', user ? user.uid : '')
            .where('accepted', '==', boolValueOfAccepted))
          .snapshotChanges()
          .pipe(
            map(actions => {
              return actions.map(a => {
                const data = a.payload.doc.data() as Match;
                const matchId = a.payload.doc.id;
                return { matchId, ...data };
              });
            })
          );
      })
    );
  }

这将产生以下返回结果:

(method) MatchStoreService.queryMatches(fieldNameOfRole: any, boolValueOfAccepted: any): Observable<{
    initiatorID: string;
    partnerID: string;
    matchedOffer: string;
    accepted: boolean;
    id: string;
}[]>

这是我尝试将其与第二个可观察对象结合的方式:

queryMatches(fieldNameOfRole, boolValueOfAccepted) {
    return this.authService.user$.pipe(
      switchMap(user => {
        return this.angularFirestore
          .collection('matches', ref => ref.where(fieldNameOfRole, '==', user ? user.uid : '')
            .where('accepted', '==', boolValueOfAccepted))
          .snapshotChanges()
          .pipe(
            map(actions => {
              return actions.map(a => {
                const data = a.payload.doc.data() as Match;
                const matchId = a.payload.doc.id;
                return { matchId, ...data };
              });
            })
          );
      }),
      map(matches => {
        return matches.map(match => {
          return this.userStoreService.getUserById(match.partnerID).pipe(
            map(user => {
              return { ...match, ...user };
            })
          );
        });
      })
    );
  }

这将产生以下返回结果:

(method) MatchStoreService.queryMatches(fieldNameOfRole: any, boolValueOfAccepted: any): Observable<Observable<{
    uid: string;
    firstname: string;
    lastname: string;
    dateOfBirth: Date;
    sex: string;
    city: string;
    activities: string[];
    offers: string[];
    mail?: string;
    ... 4 more ...;
    matchId: string;
}>[]>

更新

我如何实现getUserById():

getUserById(uid) {
    return this.angularFirestore
    .collection<any>(`users`)
    .doc<User>(uid).valueChanges();
   }

1 个答案:

答案 0 :(得分:1)

只需要像这样使用forkJoin运算符

 map(matches => {
        return combineLatest(matches.map(match => {
          return this.userStoreService.getUserById(match.partnerID).pipe(
            map(user => {
              return { ...match, ...user };
            })
          );
        }));
      })