我有一个使用AngularFireDatabase.list()
函数从我的谷歌云实时数据库中检索到的密钥列表,我想对每个密钥运行查询以获取与它们相关的信息。我一直在 forEach 循环中使用 AngularFireDatabase.object('path/${key}')
来做到这一点。如何将所有这些单独的查询合并到可迭代的可观察对象中,例如类型 Observable<Array<any>>
,以便我可以在我的 html 模板中使用async
管道?
到目前为止我所拥有的示例:
this.db.getEditors().snapshotChanges().subscribe(userRoles => {
this.activeEditors = Observable.from(userRoles.map(x => x.key)).flatMap(x => this.db.afDb.object(`/users/${x}`)
.valueChanges()).filter(x => x !== null).map(x => [{key : x.value}]);
});
其中this.activeEditors
属于类型 Observable<Array<any>>
.HTML:
<ion-list>
<ion-item *ngFor="let editor of activeEditors | async">
<ion-title>{{editor.displayName}}</ion-title>
<ion-label>{{editor.email}}</ion-label>
</ion-item>
</ion-list>
im 在此代码中遇到的问题是最终.map()
调用仅返回最后一个对象的数组。我怎样才能将所有值放入 this.activeEditors
变量中,而不仅仅是最后一项?
this.activeEditors = this.db.getEditors().snapshotChanges().switchMap(editors =>
Observable.combineLatest(editors.map { editor =>
this.db.afDb.object(`/users/${editor.key}`)
.valueChanges()
.map(...)
})
).filter(...);
在snapshotChanges()
上使用switchMap
,然后combineLatest
进一步获取的映射。 从那里filter
。