I’m missing something that’s probably super-obvious in my Twitch.tv JSON API project.
Here’s my pen: https://codepen.io/ogdendavis/pen/QaOGXx
The issue is with the Javascript object in which I’m attempting to store all the stream data responses from Twitch. When I send it to the console, I can see that the data for each stream is present. However, when I try to access the information within the object in my Javascript, I get “undefined.” I also only see a series of empty objects keyed to the Twitch usernames when I print the object in a div on the page.
Here’s the code that I think is relevant:
var userArray = ["ESL_SC2", "OgamingSC2", "cretetion", "freecodecamp", "storbeck", "habathcx", "RobotCaleb", "noobs2ninjas"]; //provided by FCC as active users
var userCount = userArray.length;
var streamResponses = {};
//loop through userArray, get stream data, and store it in streamResponses
for (i=0; i<userCount; i++) {
currentUser = userArray[i];
streamResponses[currentUser] = {};
getStreams(currentUser);
}; //end for
function getStreams(currentUser) {
call = new URL("https://wind-bow.glitch.me/twitch-api/streams/" + currentUser);
$.getJSON(call, function(result) {
streamResponses[currentUser] = result;
});
}; //end getStreams
When I call streamResponses in the console, I see this:
When I print out streamResponses into a random <div> in the document itself (as a test), I see this:
I’m certain I’m missing something that should be obvious, here. Who can help me become less ignorant about this?!