I have searched extensively for a solution to my error, but I couldn't find anything that matches exactly. I have tried solutions for similar errors, but they have not worked either.
The JSON data is structured like this:
[
{
"somedata": "value",
"somedata2": "somevalue1",
"somedata3": "somevalue2",
},
etc etc
Seems like a typical JSON format.
Working Typescript snippet:
let g = json[1]
console.log(g.somedata)
This code correctly outputs the property value: "value"
Non-working Typescript snippet:
while (a < 20) {
console.log(json[a].name)
a++;
}
This block of code does not work either
for (var index = 0; index < 2; index++) {
let g = json[index].name
console.log(g)
}
Even this snippet fails to work
var a = 1;
console.log(json[a].name)
In all cases above removing the .name property results in "Undefined" except for the first sample which works and gives me the complete first JSON object.
The error message is consistent across all examples:
core.js:6210 ERROR TypeError: Cannot read property 'name' of undefined
at GamesService.FetchAllGamesFromJSON (games.service.ts:16)
at HomeComponent.FetchGameData (home.component.ts:12)
at HomeComponent_Template_button_click_0_listener (home.component.html:1)
at executeListenerWithErrorHandling (core.js:15265)
at wrapListenerIn_markDirtyAndPreventDefault (core.js:15303)
at HTMLButtonElement.<anonymous> (platform-browser.js:582)
at ZoneDelegate.invokeTask (zone-evergreen.js:406)
at Object.onInvokeTask (core.js:28540)
at ZoneDelegate.invokeTask (zone-evergreen.js:405)
at Zone.runTask (zone-evergreen.js:178)
If anyone can identify what I am doing wrong here, please let me know!