I am working on Angular2 component in which I have declare array of string and initialise same time, in this component, in one of method I trying to push data but getting error of undefined, not sure what I am missing here!
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