Getting Facebook Avatar in Meteor when Autopublish is removed

十年热恋 提交于 2019-12-12 06:19:22

问题


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...

On the Server side:

 Meteor.publish('userData', function() {
        if(!this.userId) return null;
        return Meteor.users.find(this.userId, {fields: {
            'services.facebook': 1
        }});
    });

On Iron Router:

Router.configure({
    waitOn: function() {
        return Meteor.subscribe('userData');
    }   
 });

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.


回答1:


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

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

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="" />



回答2:


I believe you're trying to access the first_name field from the services subdocument. It should be {{currentUser.services.facebook.first_name}}

If you want to transfer first_name to the profile subdocument, you can have the following event handler:

Accounts.onCreateUser(function(options, user) {
  // ... some checks here to detect Facebook login
  user.profile.firstName = user.services.facebook.first_name;
  user.profile.lastName = user.services.facebook.last_name;
});


来源:https://stackoverflow.com/questions/29056552/getting-facebook-avatar-in-meteor-when-autopublish-is-removed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!