获取流行的Facebook朋友列表 [英] Fetching Facebook Friends List with Meteor

查看:162
本文介绍了获取流行的Facebook朋友列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我添加了meteorhacks:npm包和安装的fbgraph使用:

  $ npm install fbgraph 

我的服务器端代码现在看起来像这样:

  function Facebook(accessToken){
this.fb = Meteor.npmRequire('fbgraph');
this.accessToken = accessToken;
this.fb.setAccessToken(this.accessToken);
this.options = {
超时:3000,
池:{maxSockets:Infinity},
标头:{connection:keep-alive}
}
this.fb.setOptions(this.options);
}
Facebook.prototype.query = function(query,method){
var self = this;
var method =(typeof method ==='undefined')? 'get':方法;
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() .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});
});

我的config.js也是按照我的想法:

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

在客户端我有一个模板:

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

我试图用getFriendsList调用:

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

最后,我的packages.json看起来像这样:

  {
fbgraph:1.1.0
}

当我尝试运行我的应用程序时,我会收到如下错误:

 异常同时模拟调用'getFriendsData 
的效果TypeError:Meteor.npmRequire不是一个函数



解决方案

你需要添加npm模块。 npm模块的集成并不是流行的流星:npm模块。使用以下命令安装:

 流星添加meteorhacks:npm 

每当你通过npm添加一个非流星包,你将不得不使用 Meteor.npmRequire()。如果您通过 meteor添加foobar 安装,则不需要要求包。



如果您有问题,如果您使用Meteor 1.2,请尝试:

  rm -rf packages / npm-container 
流星删除npm-container
流星更新流星:npm

此外,您的模板需要修复,因为目前不会根据您的Meteor.call()更新。如果您使用onCreated()或onRendered(),您可以触发Meteor.call()并设置一个会话变量,将由您的一个助手用于填充您的模板:



$ {code} 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');
}
}) ;

如果您没有收到任何内容,请更改此项以检查是否在服务器端:

  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);
返回数据;
}


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.

解决方案

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;
}

这篇关于获取流行的Facebook朋友列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆