I have problem with display all products from a given category in HTML template. When the page loads it shows me this problem in the chrome console:
Error: Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Array
My service:
getCategoriesById(id) {
return this.http.get<Categories[]>('http://localhost:61085/api/Categories/GetCategoriesById/' + id);
}
My component:
export class CategoriesDetailsComponent implements OnInit {
listCategories: Categories[] = [];
constructor(private activatedRoute: ActivatedRoute, private productService: CategoriesProductsService) { }
ngOnInit() {
this.displayProductsByCategoryId();
}
displayProductsByCategoryId() {
const id = +this.activatedRoute.snapshot.params['id'];
this.productService.getCategoriesById(id).subscribe((data) => {
this.listCategories = data;
console.log(this.listCategories);
});
}
}
HTML template:
<div class="row">
<div class="col-sm-4" style="cursor: pointer;" *ngFor="let categories of listCategories " [routerLink]="['/products-details', categories.Id]" >
<div *ngFor="let product of categories.Product">
<h3 style="text-align: center">Product Name: {{product.Product.Name}}</h3>
<img class="img-fluid" src="{{product.Product.Image}}">
<p style="color: yellow">Price: {{product.Product.Price}}PLN</p>
</div>
</div>
Models class Categories and Product:
export class Categories {
Id: number;
Name: string;
Product: Products;
}
export class Products {
Id: number;
Name: string;
Description: string;
DetailedDescription: string;
Price: string;
IsNewProduct: boolean;
PromotionalProduct: boolean;
Image: string;
CategoryId: number;
}
Any help or suggestion is welcome.