AngularFireStore return collection with subquery

偶尔善良 提交于 2019-12-11 18:27:25

问题


I have the following function

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);
          })
        )
      );
  }

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.

Something like

  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);
              })
            );
          })
        )
      );
  }

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[]>

Thanks for help


回答1:


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);
              })
            ));
          })
        )
      );
  }



回答2:


You need to use mergeMap instead of map

something like below

getParticipations(
    meetingId: string
  ): Observable<Participation[]> {
    return this.meetingCollection
      .doc(meetingId)
      .collection<ParticipationDto>('participations')
      .snapshotChanges()
      .pipe(
        mergeMap(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);
              })
            );
          })
        )
      );
  }


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(
              mergeMap(pp => {
                return new Participation(id, participationDto.vart, pp.data() as Participant);
              })
            );
          })
        )
      );
  }

(since it's complicated and nested and no stackblitz provided, I am not able to verify the solution, however if it doesn't work let me know to delete this answer)



来源:https://stackoverflow.com/questions/55012380/angularfirestore-return-collection-with-subquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!