Currently, I am in the process of developing an angular application. Within my component's .ts
file, there exists an array structured as follows:
public myArray = [];
public dataFromAPI = [];
In a particular method within this component, whenever I attempt to add an element to the said array, I encountered the following error message:
this.appService.getData.subscribe(resp => {
if (resp != null) {
this.dataFromAPI = resp;
this.dataFromAPI.forEach(element => {
this.myArray.push(element)
})
}
RROR TypeError: Cannot read properties of null (reading 'push')
No obvious mistakes are apparent in the code I've written. Normally, initializing a new array in this manner does not lead to any errors. How can I go about resolving this issue?