Struggling with this one, I've hit my head against all corners but something seems to be missing.
Details
I'm working on a small JavaScript page that retrieves a Facebook photo ID, calculates the total number of people who shared it, identifies the individuals who shared it, finds the number of likes for each share, and associates them accordingly. Essentially, it's a "Calculate Shared Likes" feature.
So far, I've been able to fetch the total shares and their respective likes. However, there's an issue with arrangement due to the fact that obtaining shares requires a separate FB.API call than fetching likes.
Objective
My aim is to capture the shared image ID (used for retrieving total likes), person's name, person's ID, and store them as objects within an array. Following this, I plan to loop through the array, append the like count while ensuring they match the correct person ID.
Ultimately, my final data array should consist of entries structured like:
[{"id": "pageID_PostID", "name": "Some Name", "idmin": "This is the person ID", "likes": "Total number of likes"}, {etc}, {etc}]
Challenge
The issues I'm facing are as follows:
- Even though I have globally defined the array, pushing objects to it outside the setting function results in an empty array upon logging. Conversely, logging inside the function displays the expected elements.
- Integrating the getLikes function along with finding shares causes inconsistency or undefined responses due to differing FB.api calls and delays in result retrieval.
- Since the goal is to present the data neatly within an HTML structure, arranging the outcome as an array of objects is necessary for easier handling without causing confusion.
CODE
var pageAccessToken;
var shares = [];
function getShares(id) {
// Function logic here
}
function getLikes(pageID, idmin) {
// Function logic here
}
for (var i = 0; i < shares.length -1; i++) {
getLikes(shares[i].id, shares[i].idmin);
}
getShares(<i>insert a photo ID here</i>);
console.log("Outside call for shares array: " + JSON.stringify(shares));