skip to Main Content

I am using meteor/react for learning facebook graph api.
I want to access users’ post on facebook timeline and display them on screen. How can that be done?

With the guidance of the solution provided here [How to perform common FB actions using Meteor?. I have tried the following code: server.js

Meteor.methods({
'seePost' : function(){
  var graph=Npm.require('fbgraph');
  if(Meteor.user().services.facebook.accessToken){
    graph.setAccessToken(Meteor.user().services.facebook.accessToken);
    var future = new Future();
    var onComplete = future.resolver();
    graph.get('/me/feed',function(err,result) {
            console.log(result);
            return onComplete(err,result);
    })
   Future.wait(future);
  }
  else{
    return false;
  }

}
});

client side code :
Meteor.call("seePost", function(err,result) {
if(err) console.log("error" , err);
else console.log("RES", result);
});

I expect the result displayed in the client side console since I want to show the users the posts on his/er timeline, But I get following output :
RES, undefined

3

Answers


  1. You can do it using await and Meteor.callAsync

    Basically the client code waits for the call to complete, and gives you the returned data

    const result = await Meteor.callAsync("seePost");
    

    Errors should be handled with a try..catch block

    Login or Signup to reply.
  2. If you use fibers/future, you need to return something with “future”.

    const future = new Future();
    // some code getting result or something 
    future.return(something);
    return future.wait();
    

    this will return something in the callback from client call.

    Login or Signup to reply.
  3. try this code, when you’re using fibers you need to “wait” for the response

    Meteor.methods({
      'seePost': function () {
        var graph = Npm.require('fbgraph');
        if (Meteor.user().services.facebook.accessToken) {
          graph.setAccessToken(Meteor.user().services.facebook.accessToken);
          var future = new Future();
          var onComplete = future.resolver();
          graph.get('/me/feed', function (err, result) {
            console.log(result);
            if (err) {
              return future.return(false);
            } else {
              return future.return(result);
            }
          })
          return future.wait();
        }
        return false;
      }
    });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search