I created a function that makes a series of API calls through a Promise like this
//userApiList is a variable with an array of links
return Promise.all(userApiList.map(url =>{
var a = $.getJSON(url);
//userData is a global variable
userData.push(a);
return a;
}));
I want to both save the data to a variable for later use and return it right away to be iterated into html with jquery. Everything loads up perfectly as expected, however when I go to access the data in the variable I get all "undefined" properties. I console.logged the variable and it shows the data is there, I click the buttons well after the data is fetched so it's not an async issue. here's an example of one of the functions
$("#load_online").click(function(){
var users = [];
for(var i = 0; i < userData.length; i++){
var status = userData[i].status;
if(status !== null || status !== undefined){ users.push(userData[i]); }
}
$("#result_frame").empty();
//loadUsers() iterates the data into html
loadUsers(users);
});
I tried console.log(userData[i].status)
just to see what the results would be and I got 200
as a response when it's suppose to be null'
or the title of an episode being streamed on the channel.
The problem is I thought the responseJSON:
field is what's always returned for use, as I've never walked into this issue before. This time it seems the whole object is being read therefore userData[i].status
was reading the status:
property a layer up rather than inside the responseJSON:
object. I tried thinkering with getting a response out of userData[i].responseJSON.status
and that returned undefined for each object. Does anybody readily see what I'm doing wrong? here's a CodePen of the overall project if you need a closer look at things.