Hello,
I think I’m having a brainfart. I need to access display_name, which in this instance is “OgamingSC2.” I am using the notation globalUserData.users[0].display_name and I get the error “globalUserData.users[0] is undefined.” The screenshot is taken from a console.log(globalUserData) so I’m at least sure the globalUserData part is correct. Codepen: https://codepen.io/owainandersdigital/pen/WzYqRE?editors=1010
Logging globalUserData.users is succesful, but globalUserData.users[0] returns undefined, so it’s the array index giving me hassle. I feel like such a n00b, I’m sure this is elementary stuff!
I think sharing your codepen or actual code would help
Codepen article: https://codepen.io/owainandersdigital/pen/WzYqRE?editors=1010
I’ll add it into the main article. Thanks for replying
The console.log
runs before the $.getJSON
returns any value. $.getJSON
should be running asynchronously. So the console.log
runs while $.getJSON
waits for server reply.
You will need to write your console.log
in the callback function.
$.getJSON(apiUrl + apiType.streams + user + callback, data => {
globalUserData.streams.push(data);
console.log(data);
});
You are 100% correct. I tried moving the console.log into the getData function and it was successful. Weirdly though, two lines earlier, on line 46 I log the entire globalUserData object and it is successful.
On my console, I get: (using CodePen console, not browser’s console)
Object {
channels: [],
streams: [],
users: []
}
This is not an error because you had already defined the globalUserData.
You can try this instead?
(I’ve not tried this myself but worth a try if you prefer to keep your current code)
Thank you very much, I’ve managed to pull back data using $.ajax (then promptly didn’t save. Having a blonde day). I’m working on it again now, seems pretty straight forward.