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.
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);
}