AngularFireStore 使用子查询返回集合 [英] AngularFireStore return collection with subquery
问题描述
我有以下功能
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 丰富我的对象映射并保持我的 Observable
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[]>
感谢帮助
推荐答案
您可以尝试在 Observables 的内部列表上使用 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 使用子查询返回集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!