I have a service that is to chain a set of http requests and return a list of products. This is what I have so far:
listForDisplay(categoryId: string): Observable<any[]> {
if (!categoryId) return of([]);
return this.listFields(categoryId);
}
private listFields(categoryId: string): Observable<any[]> {
return this.fieldService.list(categoryId, true).pipe(
flatMap((fields: Field[]) => {
if (!fields) return of([]);
return this.listProducts(categoryId, fields);
}),
);
}
private listProducts(categoryId: string, fields: Field[]): Observable<any[]> {
return this.productService.listSpecificationProducts(categoryId).pipe(
flatMap((products: any[]) => {
if (!products) return of([]);
return this.mapVisibleFields(fields, products);
}),
);
}
private mapVisibleFields(fields: Field[], products: any[]): any[] {
console.log(products);
return this.productModelService.mapProductsWithSpecificationFields(fields, products);
}
In my controller I call this:
private getCategory() {
this.selectorSubscription = this.selectorService.category.subscribe(categoryId => {
this.products = [];
this.specificationService.listForDisplay(categoryId).subscribe((products: any[]) => {
console.log(products);
this.products = products;
});
});
}
The problem is that the console.log
in the first part returns an array.
The console.log
in the controller only returns a single item....
Like this:
I am clearly doing something wrong. Can anyone help?