My json looks as follows:
{
"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 I want to populate with it my angular 8 website.
In my api.service.ts I have:
getMyStructure(): Observable<HttpResponse<MyStructure[]>> {
return this.http.get<MyStructure[]>(
localUrl, { observe: 'response' });
}
and in app.components.ts I have:
myStructure: MyStructure[] = [];
getMyStructure() {
this.api.getMyStructure()
.subscribe(function(resp) {
console.log(resp.body.length); //this prints undefined
console.log(resp.body) // this prints {content: Array, page: 0, size: 30, totalElements: 1, totalPages: 1, …}
this.myStructure = resp.body;
});
console.log("after");
console.log(this.myStructure.length); //this prints 0
Now, myStructure is an interface, that contains:
export interface Shelter {
id: number;
name: string;
email: string;
img_url: string;
field1: string[];
field2: string[];
creationDateTime: string;
}
but the data inside myStructure somehow is not populated. I have no idea why, can you help me with that?