I'm new to Angular and have a simple question. In my code, I have the following:
public jsonDataResult: any;
private getUrl = "../assets/data_3.json";
getScoreList(){
this.http.get(this.getUrl).subscribe((res) => {
this.jsonDataResult = res;
console.log('Result: ', this.jsonDataResult.length);
})
}
onScoreFetch(){
this.getScoreList();
console.log('New Result : ' + this.jsonDataResult.length)
}
ngOnInit(){
this.getScoreList();
this.onScoreFetch();
}
The data displays correctly on my HTML page, but now I need to write a function using this data. Unsure of how to proceed, I attempted writing the onScoreFetch function. As a test, I wanted to see if displaying the length (this.jsonDataResult.length) would work outside of the getScoreList() function. Unfortunately, it did not work within the OnScoreFetch() method/function. Any suggestions?
I tried the following:
onScoreFetch(){
this.getScoreList();
console.log('New Result : ' + this.jsonDataResult.length)
}
While I expected "New Results" to show the length of "jsonDataResult," instead I received an error:
Error TypeScript: Cannot read properties of undefined (reading 'length')