I've been trying to re-write my code all day and keep ending up in the same place. When I add console logs to this request it seems that although I have set async: false
the code is still running asynchronously.
var likePageId = facebookData.id;
var postArray = [];
var postIdArray = [];
var testArray = [];
var check = 0;
if (facebookData.hasOwnProperty('posts')) {
facebookData.posts.data.forEach(function(post) {
if ('likes' in post) {
if ('paging' in post.likes) {
post.likes.data.forEach(function(like) { /
testArray.push(like.id + ' ' + post.id);
});
nextPage = post.likes.paging.next;
var i = 0;
do {
$.ajax({
async: false,
type: 'GET',
url: nextPage,
success: function(newPageData) {
newPageData.data.forEach(function(like) {
testArray.push(like.id + ' ' + post.id);
});
if ('paging' in newPageData.data) {
if ('next' in newPageData.data.paging) {
nextPage = newPageData.paging.next;
console.log('NEXT PAGE REASSIGNED')
}
}
currentDataLength = newPageData.data.length;
console.log('CURRENT DATA LENGTH! ' + currentDataLength)
console.log(i);
}
});
i += 1;
} while (currentDataLength != 0 && i < 5);
}
console.log(post.likes)
}
})
postArray.push(facebookData.posts.data);`
My current aim is to take data from a specified facebook pages posts. Once I have worked out how to paginate through all of the likes for a specific post, I would like to paginate through all of the posts (since a specified date) and paginate through their likes, sending User_id, post_id, page_id to a CSV file.