criso / fbgraph

NodeJs module to access the facebook graph api
http://criso.github.io/fbgraph/
1.09k stars 176 forks source link

A problem in use of FBGraph with wait.for #89

Closed SoftBrain001 closed 8 years ago

SoftBrain001 commented 8 years ago

Hi everyone, I am beginner in javascript. I'm working on a Meteor JS application, that use Facebook API. I used your API, to go faster. Initially, everything works fine, to retrieve user feed, page feed, etc ... But I encountered a problem with the method : extendAccessToken. My code :

...
// Make an API Call to get an extented token
var params = {
    "client_id":      Meteor.settings.facebookAppId
  , "client_secret":  Meteor.settings.facebookSecret
};
var facebookRes = waitFor.for(FBGraph.extendAccessToken, params);

// Store the new token, and the time of the extend operation
Meteor.users.update( userID, 
    { $set : {"services.facebook.lastTokenExtend" : new Date().getTime()
            , "services.facebook.accessToken" : facebookRes.access_token } }
);
...

Error log :

Exception while invoking method 'getUserFeed' TypeError: Object #<Object> has no method 'get'
    at exports.extendAccessToken (C:\meteor-src\*******\packages\npm-container\.npm\package\node_modules\fbgraph\lib\graph.js:452:17)
    at Object.wait.applyAndWait (C:\meteor-src\*******\packages\npm-container\.npm\package\node_modules\wait.for\waitfor.js:46:12)
    at Object.wait [as for] (C:\meteor-src\*******\packages\npm-container\.npm\package\node_modules\wait.for\waitfor.js:62:21)
    at _getFacebookUserFeed (app\server\facebook-api.js:52:32)
    at [object Object].Meteor.methods.getUserFeed (app\server\facebook-api.js:7:10)
    at maybeAuditArgumentChecks (packages/ddp/livedata_server.js:1617:1)
    at packages/ddp/livedata_server.js:648:1
    at [object Object]._.extend.withValue (packages/meteor/dynamics_nodejs.js:56:1)
    at packages/ddp/livedata_server.js:647:1
    at [object Object]._.extend.withValue (packages/meteor/dynamics_nodejs.js:56:1)

I

As the log says, the problem is with this function :

exports.extendAccessToken = function (params, callback) {
    var self = this;

    params.grant_type = 'fb_exchange_token';
    params.fb_exchange_token = params.access_token ? params.access_token : self.getAccessToken();

    self.get("/oauth/access_token", params, function(err, res) {
      if (!err && !params.access_token) {
        self.setAccessToken(res.access_token);
      }

      callback(err, res);
    });
};

Apparently, the "this" pointer contains a false value, and I do not know how to explain that. I solved the problem by adding a global variable "thisObj", that i set in "setAccessToken" method (because I always call it before using the extendAccessToken method), and I adapted the code of "extendAccessToken" like this :

exports.extendAccessToken = function (params, callback) {
    var self = thisObj;
    ....

    self.get("/oauth/access_token", params, function(err, res) {
      if (!err && !params.access_token) {
        thisObj.setAccessToken(res.access_token);
      }
      ...
    });
};

Is it possible to tell if I have erred in using the library in this way? If this is the case, how to use FBGraph synchronously?

I do not know if I should post this kind of problem here, I have not found your email address to send the message directly. Sorry in advance, if it does. And thank you for your help.

SoftBrain001 commented 8 years ago

I should use bindEnvironnement to wrap the callback, before using it.