【问题标题】:Joining two firebase observables加入两个 firebase observables
【发布时间】:2017-04-08 15:29:39
【问题描述】:

我正在使用 angularfire 数据库。我正在尝试加入我的产品和我的产品分支。我想从我的产品分支获取产品的数量。 Database 0 是我的分支的 KEY

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

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

【问题讨论】:

标签: angular firebase rxjs


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2017-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-22
    • 2018-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多