Facebook API with Node.js

I am newbie in Node JS and Mongo DB anyway I 'm trying to build an application to get facebook users id , username,gender and age using Node.js and Facebook API calls like as code below but it returns only user id.

var https = require('https');

exports.getFbData = function(accessToken, apiPath, callback) {
  var options = {
    host: 'graph.facebook.com',
    port: 443,
    path: apiPath + '?access_token=' + accessToken,
    method: 'GET'
  };
  var buffer = '';
  var request = https.get(options, function(result){
    result.setEncoding('utf8');

    result.on('data', function(chunk){
      buffer += chunk;
    });

    result.on('end', function(){
      callback(buffer);
    });
  });

  request.on('error', function(e){
    console.log('error from facebook.getFbData: ' + e.message)
  });

  request.end();
}

Thanks

callback(buffer);

doesn’t look right. Callbacks are functions but it looks like you’re defining buffer as a string instead of a function.

https://docs.nodejitsu.com/articles/getting-started/control-flow/what-are-callbacks/

A callback is a function called at the completion of a given task; this prevents any blocking, and allows other code to be run in the meantime.

Nah, I think that’s ok. I don’t have much experience of the FB graph API, but I assume callback will be a function reference which is being called and passed buffer as a parameter.

I would guess that the problem is more to do with the apiPath variable. There’s a similar question on SO, here.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.