【发布时间】:2021-10-11 12:18:35
【问题描述】:
我正在尝试从一个订阅中获取数据,并且基于数据,我正在调用嵌套订阅以添加到数据对象中。这就是我的函数的样子:
this.subCatSubscription = this.afDatabase.list('/sub-categories/').snapshotChanges().subscribe((subCat: any) => {
this.categories = [];
subCat.forEach( sc => {
if(sc.payload.val().category) {
this.cat1Subscription = this.afDatabase.list('/categories/').snapshotChanges().subscribe((cat: any) => {
cat.forEach(c => {
if(c.key === sc.payload.val().category) {
this.categories.push({ key: sc.key, ...sc.payload.val(), parentCatName: c.payload.val().name });
//this.categories.sort((a, b) => a.name.localeCompare(b.name));
}
});
});
} else {
this.categories.push({ key: sc.key, ...sc.payload.val() });
//this.categories.sort((a, b) => a.name.localeCompare(b.name));
}
console.log(this.categories)
});
})
有没有什么办法可以把上面的两个订阅结合起来,根据我的条件得到想要的数据?
谢谢。
【问题讨论】:
-
每当我看到 forEach 时,我都会想到 forkJoin 运算符。
标签: javascript firebase rxjs subscription