Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typescript: error TS2740: Type '{}' is missing the following properties from type 'Product[]': length, pop, push, concat, and 26 more

Using Angular 4, I am getting array list from firebase db inside an observable. How to change the type of an observable to an array or vice versa to make it work?

There is no different function that i have written. I am using an inbuilt function switchMap, inside which am assigning a Products array. I am not able to figure out how to change or make switchMap observable into array observable.

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 returns result for one item at a time, here there is an array list. How do I make it work.

like image 510
Pinka Avatar asked Mar 04 '23 16:03

Pinka


1 Answers

I figured it out. :) I changed the return type of the methods in product.service.ts as an observable product array.

 getAll(): Observable<Product[]>{
 return this.db.list('/products');//to get list of all products
}
 get(productId): Observable<Product[]>{
 return this.db.object('/product/' + productId);
}
like image 101
Pinka Avatar answered Apr 26 '23 21:04

Pinka