When I receive a response from an endpoint, it looks like this:
{
"data": [{
"volume": 4.889999866485596,
"name": "Carton03",
"weight": 5.75,
"storage": 3
}, {
"volume": 2.6500000953674316,
"name": "Carton02",
"weight": 4.5,
"storage": 2
}, {
"volume": 1.4500000476837158,
"name": "Carton01",
"weight": 5,
"storage": 1
}],
"response": "true",
"type": "Storages"
}
In the following code snippet, I attempt to create an array in my component:
export class StorageComponent {
private data: any;
private stors: Storage [];
constructor (private storageService: StorageService ) {}
storage () {
this.data = this.storageService.Storage();
//storageService.Storage is an assistant service that parses the response
// and returns only the data-array of the response
for (let i = 0; i < this.data.data.length; i++) {
const storages = this.data.data[i];
console.log(storages.storage);
console.log(storages.name);
console.log(storages.weight);
console.log(storages.volume);
this.stors[i] = storages;
}
}
}
I utilize a const called 'storages' to inspect and evaluate the data, which works correctly.
The issue arises when I try to populate my 'stors' variable, which is an array of the Storage model with attributes like storage, name, etc.
On the last line where I'm trying to do this, I encounter the error:
Cannot set property '0' of undefined
Does anyone have any ideas on how to resolve this?