Here is the code I am currently working with:
data:IQuest[]|any=[];
ngOnInit(){
this.getData();
console.log(this.data[1].Question);
}
getData(){
let url = "http://127.0.0.1:5153/Quests";
this.http.get(url).subscribe(data=>{
this.data=data;
});
}
export interface IQuest {
Id: number,
lat: number,
lon: number,
Question:string,
}
I'm having trouble accessing values inside the data property in typescript. When I try to call console.log, as shown in the code above, I encounter an error that says:
ERROR TypeError: Cannot read properties of undefined (reading 'Question')
at AppComponent.ngOnInit (app.component.ts:32:30)
However, when I attempt to display the values in HTML, they show up properly without any issues with access, for example:
<div *ngFor="let element of data">
{{element.question}}
</div>
This code lists the value of the 'question' property one after another. Can you help me understand why I can't access those values in TypeScript?