Fetching Facebook Friends List with Meteor

2019-05-29 02:25发布

I added the meteorhacks:npm package and installed fbgraph using:

$ npm install fbgraph

My server side code looks like this for now:

function Facebook(accessToken) {
    this.fb = Meteor.npmRequire('fbgraph');
    this.accessToken = accessToken;
    this.fb.setAccessToken(this.accessToken);
    this.options = {
        timeout: 3000,
        pool: {maxSockets: Infinity},
        headers: {connection: "keep-alive"}
    }    
    this.fb.setOptions(this.options);
}
Facebook.prototype.query = function(query, method) {
    var self = this;
    var method = (typeof method === 'undefined') ? 'get' : method;
    var data = Meteor.sync(function(done) {
       self.fb[method](query, function(err, res) {
           done(null, res);
       });
   });
   return data.result;
}
Facebook.prototype.getUserData = function() {
return this.query('me');
}
Facebook.prototype.getFriendsData = function() {
    return this.query('/me/friendlists');
}
Meteor.methods({
    getUserData: function() {
       var fb = new Facebook(Meteor.user().services.facebook.accessToken);
       var data = fb.getUserData();
       return data;
       },
    getFriendsData: function() {
       var fb = new Facebook(Meteor.user().services.facebook.accessToken);
       var data = fb.getFriendsData();
       return data;
    }
});

Meteor.publish("getUserData", function () {
    return Meteor.users.find({_id: this.userId});
});

Meteor.publish("getFriendsData", function(){
    return Meteor.users.find({_id: this.userId});
});

My config.js is also in order I think:

Accounts.ui.config({
    passwordSignupFields: "USERNAME_ONLY",
    requestPermissions: {
        facebook: ['email', 'user_friends'],
    }
});

On the client side I have a template:

<template name="friends">
    <div class="container">
        {{friendlist}}
    </div>
</template>

And I'm attempting to call 'getFriendsList' with:

Template.friends.helpers({
    friendlist: function() {
       Meteor.call("getFriendsData");
   }
});

Finally, my packages.json looks like this:

{
    "fbgraph": "1.1.0"
}

When I try to run my app, I get an error as follows:

Exception while simulating the effect of invoking 'getFriendsData
TypeError: Meteor.npmRequire is not a function

I apologize if this is a stupid question, I'm fairly new to Meteor. And I for the life of me can't figure this one out. I'd really appreciate some help.

1条回答
放荡不羁爱自由
2楼-- · 2019-05-29 02:55

You need to add the npm module. Integration of npm modules isn't native to meteor with the meteorhacks:npm module. Install it with this command:

meteor add meteorhacks:npm

Whenever you add a non-meteor package via npm, you will have to use Meteor.npmRequire(). If you install via meteor add foobar you won't need to require the package.

If you have problems, try this if you are using Meteor 1.2:

rm -rf packages/npm-container
meteor remove npm-container
meteor update meteorhacks:npm

Also your template needs fixing, as it's currently not going to update based on your Meteor.call(). If you use onCreated() or onRendered() you can trigger the Meteor.call() and set a session variable that will be used by one of your helpers to populate your template:

Template.friends.onCreated(function() {
    Meteor.call("getFriendsData", function(error, friends) {
        if (error) {
            console.log(error);
        } else {
            Session.set('friends', friends);
        }
    });
});

Template.friends.helpers({
   friendlist: function() {
       return Session.get('friends');
   }
});

If you aren't getting anything back, change this to check if you are getting data back on the server side:

getFriendsData: function() {
   console.log(Meteor.user().services.facebook.accessToken);
   var fb = new Facebook(Meteor.user().services.facebook.accessToken);
   var data = fb.getFriendsData();
   console.log(data);
   return data;
}
查看更多
登录 后发表回答