目前,当自动发布被删除时,只有 {{currentUser.profile.name}} 有效。我尝试从 Facebook 获取 {{currentUser.profile.first_name}} 和头像,但未能成功。这是我的代码...
在服务器端:
Meteor.publish('userData', function() {
if(!this.userId) return null;
return Meteor.users.find(this.userId, {fields: {
'services.facebook': 1
}});
});
在铁路由器上:
Router.configure({
waitOn: function() {
return Meteor.subscribe('userData');
}
});
根据我的理解,我看到 Meteor 正在发布所有 userData,然后通过 Iron Router 订阅它。我不明白的是为什么这不起作用——因为我认为 {{currentUser.profile.first_name}} 应该起作用,但事实并非如此。
最佳答案
就像 Richard 建议的那样,创建用户时,您可以将服务文档复制到配置文件文档中。
Accounts.onCreateUser(function(options, user) {
// We still want the default hook's 'profile' behavior.
if (options.profile) {
user.profile = options.profile;
user.profile.memberSince = new Date();
// Copy data from Facebook to user object
user.profile.facebookId = user.services.facebook.id;
user.profile.firstName = user.services.facebook.first_name;
user.profile.email = user.services.facebook.email;
user.profile.link = user.services.facebook.link;
}
return user;
});
您要获取他们的名字和 Facebook ID 的出版物将如下所示...
/* ============== Single User Data =============== */
Meteor.publish('singleUser', function(id) {
check(id, String);
return Meteor.users.find(id,
{fields: {'profile.facebookId': 1, 'profile.name': 1, 'profile.firstName': 1, 'profile.link': 1}});
});
您可以使用模板帮助程序功能访问用户的 Facebook 头像...
Template.profileView.helpers({
userPicHelper: function() {
if (this.profile) {
var id = this.profile.facebookId;
var img = 'http://graph.facebook.com/' + id + '/picture?type=square&height=160&width=160';
return img;
}
}
});
在您的模板中,您可以使用以下帮助程序(前提是您将其包装在包含用户数据的 block 中):
<img src="{{userPicHelper}}" alt="" />
关于javascript - 删除自动发布后在 Meteor 中获取 Facebook 头像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29056552/