带有子查询的AngularFireStore返回集合

带有子查询的AngularFireStore返回集合

本文介绍了带有子查询的AngularFireStore返回集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有以下功能

getParticipations(
    meetingId: string
  ): Observable<Participation[]> {
    return this.meetingCollection
      .doc(meetingId)
      .collection<ParticipationDto>('participations')
      .snapshotChanges()
      .pipe(
        map(actions =>
          actions.map(m => {
            const participationDto = m.payload.doc.data() as ParticipationDto;
            const id = m.payload.doc.id;
            return new Participation(id, participationDto.vart, null);
          })
        )
      );
  }

participationDto中有一个文档引用,我想获取该文档,以便返回带有所引用文档映射的对象(参与).

In the participationDto there is a document reference and I would like to get that document in order to return an object (participation) with a mapping of the referenced document.

类似

  getParticipations(
    meetingId: string
  ): Observable<Participation[]> {
    return this.meetingCollection
      .doc(meetingId)
      .collection<ParticipationDto>('participations')
      .snapshotChanges()
      .pipe(
        map(actions =>
          actions.map(m => {
            const participationDto = m.payload.doc.data() as ParticipationDto;
            const id = m.payload.doc.id;
            return this.participantCollection.doc(participationDto.participant.id).get().pipe(
              map(pp => {
                return new Participation(id, participationDto.vart, pp.data() as Participant);
              })
            );
          })
        )
      );
  }

但是它返回一个Observable<Observable<Participation>[]>我可能需要合并,映射或类似方法,但是我找不到正确的方法来丰富我的Observable对象映射并保持我的Observable<Participation[]>

But then it returns an Observable<Observable<Participation>[]>I probably need to merge, map or something like that but I don't find the right way to get my Observable enriched with my object mapping and keep my Observable<Participation[]>

感谢帮助

推荐答案

您可以尝试在Observable的内部列表上使用forkJoin并将外部地图切换到switchMap.

You could try using forkJoin on the inner list of Observables and switching the outer map to a switchMap.

     getParticipations(
    meetingId: string
  ): Observable<Participation[]> {
    return this.meetingCollection
      .doc(meetingId)
      .collection<ParticipationDto>('participations')
      .snapshotChanges()
      .pipe(
        switchMap(actions =>
          forkJoin(actions.map(m => {
            const participationDto = m.payload.doc.data() as ParticipationDto;
            const id = m.payload.doc.id;
            return this.participantCollection.doc(participationDto.participant.id).get().pipe(
              map(pp => {
                return new Participation(id, participationDto.vart, pp.data() as Participant);
              })
            ));
          })
        )
      );
  }

这篇关于带有子查询的AngularFireStore返回集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 07:12