加入两个firebase observables
问题描述:
我正在使用angularfiredatabase。我正在尝试加入我的产品和我的产品分支。我想从我的产品分支获得数量到产品。继承人的Database 0有我的分支加入两个firebase observables
findAllProductsForBranch(branchName:string):Observable<any[]> {
const products$ = this.findProductsByProductKeys(this.findProductKeysPerBranch(branchName));
products$.map((products) => {
products.forEach((product) => {
product.map((product) => {
Object.assign(product,
{
quantity:
this.db.object(`/products-branch/0/${product.$key}`)
.switchMap(product => product._quantity)
})
})
});
//return products;
})
.do(console.log)
.subscribe();
return Observable.of([]);
}
产品$的键包含一个可观察的产品没有数量,所以我想添加量,但问题是如何?当我运行这个未定义的代码显示在控制台中。林坚持在这里呆了近一个星期
答
我找到了答案,我的问题,这要归功于cartant他解释说在这里Fork join two firebase observables
findAllProductsForBranch(branchName:string, branchKey: string):Observable<any[]> {
const products$ = this.findProductsByProductKeys(this.findProductKeysPerBranch(branchName));
return products$.mergeMap((products) => {
return Observable.forkJoin(
// Map the tours to the array of observables that are to
// be joined. Note that forkJoin requires the observables
// to complete, so first is used.
products.map((product) => this.db
.object(`/products-branch/${branchKey}/${product.$key}`)
.first()
),
// Use forkJoin's results selector to match up the result
// values with the tours.
(...values) => {
products.forEach((product, index) => { product._quantity = values[index]._quantity; });
return products;
}
);
});
}
这是你使用的包? https://github.com/angular/angularfire2 – brennan
是的,伙计那是正确的 – JarellGabon