1

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
            });
        }
    }
});
Vahid SJ
  • 383
  • 1
  • 2
  • 12
  • make sure you understand what "asynchronous" means, it is important to know in the javascript world. also, google for "async/await", it will help you. – andyrandy Feb 25 '18 at 14:49
  • @luschn here I found an example that you already have written: https://stackoverflow.com/questions/43564239/asynchronous-method-in-while-loop-with-graph-api-paged I wanted to ask: before your code I should assign the variable FB so: var FB = require('facebook-node-sdk'); ? – Vahid SJ Feb 26 '18 at 15:36
  • honestly...if you need to ask if you should require a package in node.js, you should go through some very basic node.js tutorials first ;) – andyrandy Feb 26 '18 at 15:37
  • either way, the example in that other thread should be good enough to learn how to deal with this. – andyrandy Feb 26 '18 at 15:40
  • @luschn [shy] :) If I assign the variable var FB = require('facebook-node-sdk'); I get this error: Unhandled promise rejection (rejection id: 3): TypeError: FB.api is not a function – Vahid SJ Feb 26 '18 at 15:56
  • And according to the documentation if I assign the variable var FB = new Facebook({ appID: 'YOUR_APP_ID', secret: 'YOUR_APP_SECRET' }); I get this error: Callback cannot throw error: TypeError: Cannot read property 'next' of undefined – Vahid SJ Feb 26 '18 at 15:58
  • i suggest using the "fb" module instead. try that one. – andyrandy Feb 26 '18 at 16:13

0 Answers0