【发布时间】:2019-06-09 07:59:30
【问题描述】:
在我的 firebase (angularfire2) 应用程序中,我希望为集合的每个元素返回文档。为此,我使用 Observables + .subscribe()。但我想使用 RXJS 和 AsyncPipe 获得替代方案。
我尝试使用下一个 RXJS 链:
this.shortlisted$ = this.db.collection(`interestingStartups`).valueChanges().pipe(mergeMap(actions => actions.map(el => {
return this.db.doc(`startups/${(<any>el).startupUid}`).valueChanges().pipe(mergeMap(el => of([el])));
})));
并在 HTML 模板中使用 *ngFor="let s of shortlisted$ | async"。但它不起作用。 Angular 显示下一个错误“找不到类型为 'object' 的不同支持对象 '[object Object]'。NgFor 仅支持绑定到 Iterables,例如数组”
我当前有效的代码(Observables + .subscribe):
this.db.collection('interestingStartups').valueChanges().subscribe((el:any) => {
el.forEach(is => {
this.db.doc('startups/' + is.startupUid).get().subscribe(s => {
this.shortlisted.push(s.data());
})
})
});
我只使用没有 AsyncPipe 的纯 *ngFor,即 *ngFor="let s of shortlisted"
我想使用mergeMap 或类似的RXJS 运算符将结果分配给this.shortlisted。目前我无法找到可行的解决方案
请帮忙!
【问题讨论】:
标签: angular firebase rxjs angularfire2