类型脚本:错误TS2740:类型'{}'缺少类型'产品[]'的以下属性:长度,弹出,推入,连续和另外26个

时间:2019-07-26 07:16:13

标签: angular firebase observable switchmap

使用Angular 4,我从firebase db中获得了一个可观察的数组列表。如何将可观察对象的类型更改为数组,反之亦然,以使其起作用?

我没有编写其他功能。我正在使用内置函数switchMap,在其中分配了Products数组。我无法弄清楚如何将switchMap变为可观察到的数组或将其变为可观察到的数组。

constructor(
    route:ActivatedRoute,
    productService: ProductService ) { 
      productService
        .getAll()
        .switchMap(products => {
      //getting compile error at below line for this.products
          this.products = products;
          return route.queryParamMap;
        })
        .subscribe(params => {
          this.category = params.get('category');
          this.filteredProducts = (this.category) ? 
            this.products.filter(p => p.category === this.category) :
            this.products;
    });
  }

switchMap observable一次返回一项的结果,这里有一个数组列表。我如何使其工作。

1 个答案:

答案 0 :(得分:0)

我知道了。 :) 我在 product.service.ts 中将方法的返回类型更改为可观察产品数组

 getAll(): Observable<Product[]>{
 return this.db.list('/products');//to get list of all products
}
 get(productId): Observable<Product[]>{
 return this.db.object('/product/' + productId);
}