【发布时间】:2020-01-07 22:02:40
【问题描述】:
在colWithIds$ 下方的可观察管道中,我提供了一个可观察的 Firestore 集合(单个对象数组),我将其称为父文档。对于这些文档中的每一个,我使用flatMap 对关联文档进行一次调用,我将其称为子文档。然后我将父文档和子文档存储在局部变量中:
this.db.colWithIds$('parentCollection')
.pipe(
tap(parentDocs => this.parentDocs = _.keyBy(parentDocs, '_id')),
flatMap(parentDocs => combineLatest(parentDocs.map(doc => this.db.doc(`childCollection/${doc.childId}`).get()))),
map(snaps => convertSnaps(snaps)),
tap(childDocs => this.childDocs = _.keyBy(childDocs, '_id'))
).subscribe()
这种方法的问题:
- 有时父文档可用,但子文档不可用
- 我想把它放在一个服务中以供重用,所以以这种方式分配局部变量是有问题的。
我正在寻找一种方法,我可以等到所有值都被解析并以以下形式返回单个对象:
{
parentDocs: [{}, {}, ...], // the documents
childDocs: [{}, {}, ...], // the documents
}
我对上述问题的其他解决方案持开放态度。提前致谢。
【问题讨论】:
标签: typescript firebase rxjs google-cloud-firestore angularfire