I have been working on an Angular2 component where I declared an array of strings and initialized it at the same time. However, I encountered an error of 'undefined' when I tried to push data in one of the methods. I am unsure about what I might be missing!
export class MyComponent implements OnInit {
public categoryData: string[] = [];
ngOnInit(): void {
}
public loadDataFromServer() {
let MyServerData = result.data;
MyServerData.forEach(function (item) {
this.categoryData.push(item.BarTitle); // error here
});
}
}
error
ERROR TypeError: Cannot read property 'categoryData' of undefined