I am working on a piece of code where I aim to collect data from 2 getjson calls and store it in an array only when both calls have successfully completed. However, I encountered the following error:
resultFromUrl1.feed is undefined
var entry1 = resultFromUr11.feed.entry;
Upon inspecting the F12 console, I confirmed that both getjson calls were executed successfully, yet no data was being pushed into the array. How can I resolve this issue? Additionally, should I be using $.when(url1Promise, url2Promise).then or $.when(url1Promise, url2Promise).done?
<javascript>
var files = new Array();
function pushtoArray() {
//first getjson call
var url1 = "https://spreadsheets.google.com/feeds/list/xxxxx/xxxxx/public/values?alt=json";
var url1Promise = $.getJSON(url1, function (data) {
console.log("url1 success");
});//end of ajax call
//second getjson call
var url2 = "https://spreadsheets.google.com/feeds/list/xxxxx/xxxxx/public/values?alt=json";
var url2Promise = $.getJSON(url2, function (data) {
console.log("url2 success");
});//end of function
$.when(url1Promise, url2Promise).then(function (resultFromUrl1, resultFromUrl2) {
var entry1 = resultFromUrl1.feed.entry;
var entry2 = resultFromUrl2.feed.entry;
var entry = entry1.concat(entry2);
$(entry).each(function () {
// Column names are name, age, etc.
count++;
files.push({ url: this.gsx$url.$t, filename: this.gsx$name.$t });
alert(files.length);
print_r(files);
console.log(files);
});
});
}//end of function
</javascript>
<body onload="pushtoArray()">