2

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.

Storm
  • 35
  • 4

1 Answers1

1

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;
}
Brett McLain
  • 2,000
  • 2
  • 14
  • 32
  • I already added the meteorhacks:npm package using that command. I even tried readding npm-container with those commands. But the problem still persists. Is there anything I'm doing wrong? – Storm Jan 22 '16 at 16:12
  • Even though this is on the server side, try wrapping this explicitly with if (Meteor.isServer) { } to be sure that it's executing only on the server side, as Meteor.npmRequire() is server side only. The error you're seeing is telling us that it can't find npmRequire(), so either it's not installed correctly or it's being executed somewhere that it can't find it (i.e. client side). – Brett McLain Jan 22 '16 at 16:17
  • I tried wrapping it around if(Meteor.isServer) { } and I even reintalled meteorhacks:npm after removing npm-container. Still no luck. – Storm Jan 22 '16 at 16:23
  • It's a long shot, but try `this.fb = Npm.require('fbgraph');` Typically this is only used when you're developing a package, but I've heard this works for some people. I'm out of ideas after this :( Sorry! – Brett McLain Jan 22 '16 at 16:27
  • I just restarted my app and the error is gone. But the template is still empty. I tried running "Meteor.call("getFriendsData")" on the console but it didn't return anything. It's possible I made a mistake in my meteor code. Could you let me know if you think anything is wrong? Thanks alot for the help mate :) – Storm Jan 22 '16 at 16:34
  • Updated my answer with more information for you. Basically add console.log() on the server side to check you are getting data back, and revamp your template helper to properly return the data. – Brett McLain Jan 22 '16 at 16:42
  • The data I'm receiving just consists of an empty array. This is the case for both friends and user data. What do you reckon I should do? – Storm Jan 22 '16 at 18:07
  • @Storm I am having the same problem. Empty array. Did you or anyone ever figure this out? – Adam Moisa Dec 06 '16 at 23:23