I am doing a small assignment using Facebook API. One of my GET requests involves me send a request and storing the data for later use. Now, I managed to this while using the same data in the same call, however, this time, I would like to store the data inside a global variable to use it in other GET calls. The code I implemented is this:
var pageDataItemGlobal;
$.getJSON("https://localhost:44326/api/Facebook/GetPageFeed?accessToken=" + response.authResponse.accessToken)
.done(function (data) {
$.each(data, function (key, pageDataItem) {
pageDataItemGlobal = pageDataItem;
for (i in pageDataItem) {
console.log(pageDataItem[i].name + ', ' + pageDataItem[i].id) //Shows the values
}
});
});
console.log(pageDataItemGlobal) //shoes undefined
I search online and found that if you assign the value of the global variable to the one inside the .each() function, then you can use it. However, it does not return the values. Instead, it just returns undefined.
What am I doing wrong please? Thanks