How to do pagination using Node JS for Facebook Graph API?

814 views Asked by At

I want to extract all the posts of a specific Facebook Page. I've already written this code in Python but I don't know how to do that in Node.js.

With this code, I only extract the first page of Graph API and the loop stops. Where am I wrong in this piece of code? I think I got confused with Python :)

I would be appreciated for your help.

var graph = require('fbgraph');

var accessToken = "MyAccessToken"
graph.setAccessToken(accessToken);

graph.setVersion("2.11");

var options = {
    timeout:  3000,
    pool:     { maxSockets:  Infinity },
    headers:  { connection:  "keep-alive" }
};

graph.setOptions(options)

graph.get("5550296508?fields=posts", function(err, resp) {
    var data = resp;
    console.log(JSON.stringify(data));
    while (true){
    if(data.posts.paging && data.posts.paging.next){
        graph.get(data.posts.paging.next, function(err, res) {
            console.log(JSON.stringify(res));
            var data = res
            });
        }
    }
});
0

There are 0 answers