i am trying to return products from my backend api and display it on my frontend page. When i execute the *ngFor loop it gives me an error. Here are my codes.
My Backend API
data
0
name "perferendis"
totalPrice 323.76
rating 5
discount 43
href
link "http://localhost:8000/api/products/1"
1
name "non"
totalPrice 92.34
rating 3.5
discount 19
href
link "http://localhost:8000/api/products/2"
2
name "a"
totalPrice 246.76
rating 3
discount 38
href
"http://localhost:8000/api/products/3"
3
name "vitae"
totalPrice 537.57
rating "No rating yet"
discount 1
href
link "http://localhost:8000/api/products/4"
links
first "http://localhost:8000/api/products?page=1"
last "http://localhost:8000/api/products?page=3"
prev null
next "http://localhost:8000/api/products?page=2"
meta
current_page 1
from 1
last_page 3
path "http://localhost:8000/api/products"
per_page 20
to 20
total 60
My Service
getProducts(): Observable<any> {
return this.http.get('http://localhost:8000/api/products')
.map(res => {
return res;
});
My Component
products: Products[];
constructor(private productservice: ProductService) { }
ngOnInit() {
this.productservice.getProducts()
.subscribe(res => this.products = res);
}
}
when i do a for loop in my html file with the products property it returns this error. "Error: Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Arrays." What do i need to correct? And how do i get the products object properly displayed?
Edit
<li class="span4" *ngFor="let product of products">
that is my ngFor loop.
*ngForhas to be an array.*ngForcan't iterate objects.*ngForin your template?