I've hit my head in all corners with this one but I'm missing something here.
Info
I'm coding a small javascript page that will take a photo ID of a Facebook photo, then return the total number of people, and the people who shared it, then find the number of likes on each photo, and attach it along with that.
Basically it's a "Calculate Shared Likes"
So far I managed to query the total number of shares, and find the likes on them, but there is no sense of arrangement because the FB.API function that gets the shares is a different request than the FB.API function that will get the likes.
Goal
What I've been trying to do is get the shared image ID, (which I use to get the total likes), name of the person, person ID and push them into an array as objects.
Which then I'll for-loop the array and attach the number of likes while checking and making sure that the likes belong to the correct person ID.
So my final data array would look like:
[{"id" : "pageID_PostID", "name" : "Some Name", "idmin" : "This is the person ID", "likes" : "This is the total number of likes"}, {etc}, {etc}]
Problem
1-console.log-ing the array I'm pushing the objects in OUTSIDE of the function that set it comes back as an empty array, even though I defined the array globally. While console.log-ing it inside of the function comes back as expected.
2-Reason why I can't use the getLikes function along with the finding shares function is because it comes out either as undefined or mismatched, I assume because its a different FB.api call and takes a few milliseconds to query and get results, by which JS decides its undefined.
3-Since the goal is to lay it all formatted in an HTML, I would need to arrange the result in a nice format for me to work with without making a soup out of it. Which is the array[object, object, object]
CODE
var pageAccessToken;
var shares = new Array();
function getShares(id){
FB.api('https://graph.facebook.com/' + id +'/sharedposts?access_token='+pageAccessToken, function(response)
{
for(var i = 0; i < response.data.length; i++)
{
if(response.data[i].story.indexOf('shared')){
var shareID = response.data[i].id;
var pageID = response.data[i].from.id;
var shareObj = new Object();
console.log(response.data[i].story);
console.log(" With Post ID " + shareID);
shareObj.id = response.data[i].id;
shareObj.name = response.data[i].from.name;
shareObj.idmin = response.data[i].from.id;
shareObj.likes = null;
shares.push(shareObj);
} //end if
} //end for loop
console.log(response.data.length + " People shared this photo.");
console.log("Inside call for shares array" + JSON.stringify(shares));
return shares; //I don't really get how return works cause its not helping anywhere!
}); //end fb api
} //end getShares
//function to retrieve likes
function getLikes(pageID, idmin){
FB.api('https://graph.facebook.com/' + pageID + '/likes? summary=1&access_token='+pageAccessToken, function(response)
{
for (var i = 0; i < shares.length - 1; i++) {
shares[i].likes = response.summary['total_count'];
console.log(shares[i].name + " has " + shares[i].likes + " likes.");
console.log(response.summary['total_count']);
}
});
}
//end getLikes
for (var i = 0; i < shares.length - 1; i++) {
getLikes(shares[i].id, shares[i].idmin);
}
getShares(insert a photo ID here);
console.log("2 Outside call for shares array" + JSON.stringify(shares));