I have a returned json object:
{
"data": [
{
"type": "product",
"id": "e0267f46-9721-48fc-8ee0-b554d82cfb52",
"name": "fwefwefwef",
...and so on...
I process it with this as part of my service:
export class MService {
...
products.data.forEach(element => {
let product = {
name : element.name,
sku : element.sku,
description : element.description,
category : element.relationships.categories.data[0].id,
price: element.price[0].amount
console.log(product);
})
});
let product = MService
});
}
}
Which returns each object individually:
{
"name": "Bombay Sapphire",
"sku": "bomsaph",
"description": "A great gin",
"category": "46569361-13477074f952",
"price": 1999
}
I have a model:
export class Prod{
constructor(public name:string, public sku:string, public description:string, public category: string, public price:number){}
}
And a Component for which the *ngFor loop in the HTML returns needs to display what the API returns.
constructor (private mService: MService){
products:Prod[] = [MService]
}
HTML:
<div *ngFor="let product of products">
<div>{{product.name}} </div>
<div>{{product.description}}</div>
<div>{{product.sku}}</div>
</div>
I am getting 'unused label' and 'expression expected' errors in the component.
productsis being set to an array, which contains a reference to theMServiceclass. That doesn't seem right. You need to use the instance of theMServiceinstead:products = mService.whatever. Note that I added "whatever" b/c you don't really show the API for MService, so it's not clear where to get the array ofProdobjects from.