How can I do pagination when downloading facebook posts via javascript and FB.api? -
i trying download facebook wall posts via javascript sdk. managed call first 25 posts, trouble when trying paging.next , feed new call using loop, , iterate on until no more pages available.
the code produces same page 10 times not understand. should give next page , next page , next page..?
fb.login(function(response){ // first call calls pagename/feed fb.api(request, function (response) { // prints log , declares x next page console.log(response.paging.next); var x = response.paging.next; // loop preferably should continue until no more pages // deal later (i = 0; < 10; i++) { // calls x gives me rhe next page // reason code not manage change // x , new call fb.api(x, function (res){ console.log(i); console.log(res.paging.next); // here respecify x x = res.paging.next; }); }; } ); }, {scope: 'publish_actions'});
you need learn how deal asynchronous javascript , how recursive functions. trying use "x" before asynchronous api call sets it. meaning, whole loop finishes before "x" gets set once - because api call takes while.
here´s quick code, did not test should show 1 solution:
var items = []; function apicall(next) { fb.api(next, function (response) { (var = 0; < response.data.length; i++) { //add posts items array items.push(response.data[i]); } if (response.paging && response.paging.next) { //call function recursively until there no "next" apicall(response.paging.next); } else { //this when it´s done console.log(items); } }); } apicall('/page-id/feed');
make sure understand concepts, it´s important know when deal javascript sdk , javascript in general.
Comments
Post a Comment