移除自动出版物时,在流星中获取Facebook头像 [英] Getting Facebook Avatar in Meteor when Autopublish is removed
问题描述
目前,当自动发布被移除时,只有{{currentUser.profile.name}}有效。我想尝试从Facebook获得{{currentUser.profile.first_name}}和头像,但无法这样做。这是我的代码...
Currently when auto publish is removed, only {{currentUser.profile.name}} works.I'm trying to get {{currentUser.profile.first_name}} and the avatar from Facebook but have not been able to do so. Here is my code...
在服务器端:
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,然后通过铁路路由器订阅。我不明白为什么这不行 - 我认为{{currentUser.profile.first_name}}应该工作,但不是。
From my understanding, I see that Meteor is publishing all the userData and then subscribing to it via Iron Router. What I don't understand is why this is not working -- as I think {{currentUser.profile.first_name}} should work but isn't.
推荐答案
像Richard所说,创建用户时,您可以将服务文档复制到配置文件。
Like Richard suggests, when a user is created, you can copy the services document to the profile doc.
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将如下所示...
Your publication to get their first name and Facebook ID would look like this...
/* ============== 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头像...
You can access a user's Facebook avatar with a template helper function...
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;
}
}
});
在您的模板中,您可以使用以下帮助器(前提是您将其包装在块中包含用户数据):
In your template, you can then use the following helper (provided you are wrapping this in a block that contains user data):
<img src="{{userPicHelper}}" alt="" />
这篇关于移除自动出版物时,在流星中获取Facebook头像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!