I'm trying to display data in my Angular 8 application. My webservice returns the following json:
{
"content": [
{
"id": 1,
"name": "test name",
"email": "[email protected]",
"img_url": "url",
"field1": [
{
"id": 10,
"userId": 1,
"registeredAt": "2020-01-09T22:21:23.272",
"amount": 200
}
],
"field2": [
{
"id": 11,
"userId": 1,
"registeredAt": "2020-01-09T22:21:46.113",
"amount": 200
}
],
"creationDateTime": "2020-01-05T00:00:00Z"
}
],
"page": 0,
"size": 30,
"totalElements": 1,
"totalPages": 1,
"last": true
}
And for each element of this array I want to display a div with style card, e.g.:
<div class="card" style="width: 18rem;">
<img class="card-img-top" src="url" alt="test name">
<div class="card-body">
<p class="card-text">[email protected]</p>
</div>
</div>
The code above I put in app.component.ts.
I started from modyfing my app.component.ts and I entered there:
export class AppComponent implements OnInit {
title = 'myTestfrontend';
mystructure: MyStructure[] = [];
constructor(private api: ApiService) {}
ngOnInit() {
this.getMyStructure();
}
getMyStructure() {
this.api.getMyStructure()
.subscribe(resp => {
for (const content of resp.body) {
this.mystructure.push(content);
}
});
}
}
but it returns the error:
undefined is not a function (near '...content of resp.body...')
What's wrong here and how can I fix it so that I could display as many cards as there are entries in my json? Thanks a lot!