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 that I need to retrieve in order to return an object (participation) with a mapping of the referenced document.
Here's an update to the code:
getParticipations(
meetingId: string
): Observable<Participation[]> {
return this.meetingCollection
.doc(meetingId)
.collection<ParticipationDto>('participations')
.snapshotChanges()
.pipe(
mergeMap(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);
})
);
})
)
)
);
}
This updated code resolves the issue where it was returning an
Observable<Observable<Participation>[]>
. By using mergeMap
and forkJoin
, the Observables are merged correctly while keeping the Observable<Participation[]>
format.
I hope this helps!