加入两个火基可观测量

时间:2017-04-08 15:29:40

标签: angular firebase rxjs

我正在使用angularfiredatabase。我正在尝试加入我的产品和产品分支。我想从产品分支到产品获取数量。继承人Database 0有我的分支的钥匙

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([]);
}

产品$包含一个没有数量的可观察产品,所以我想添加一个数量,但问题是如何?当我运行此代码时,未定义的内容将显示在控制台中。我在这里待了将近一个星期

1 个答案:

答案 0 :(得分:0)

我找到了我的问题的答案,感谢他在这里解释了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;
            }
        );
    });
}