As I call the api to fetch data in the ngOnInit lifecycle hook, I am struggling with assigning the retrieved data to a class variable. Here is the relevant code snippet:
tasks: Task[];
ngOnInit() {
this.apiService.getTasks()
.subscribe( data => {
Object.keys(data).map((index) => {
this.tasks.push(data[index]);
});
});
}
An error message 'TypeError: Cannot read property 'push' of undefined' keeps appearing for the line:
this.tasks.push(data[index]);
Even though 'tasks' has been defined as an array.
I could really use some help understanding the scope here, any suggestions would be greatly appreciated.