使用助手 METEOR-JS 调用 Facebook 照片

Calling Facebook photos using helpers METEOR-JS

我正在尝试在 Meteor 中使用以下方法调用 facebook 个人资料照片。

Template.foo.helpers({
    avatar: function() {
        if (Meteor.user().services.facebook) {
        return "http://graph.facebook.com/" + Meteor    .user().services.facebook.id + "/picture/?type=large"; 
        }
        else
        {
            return "img/blank_avatar.png"
        }
    }
});

我用

来称呼它
<img src = "{{avatar}}">

在我的浏览器控制台中,我收到一条错误消息

Exception in template helper: avatar

我真的不明白这是什么问题...

让我们在 onCreateUser 方法上将 Facebook 逻辑更改为 server side

//server
    Accounts.onCreateUser(function(options, user) {
       if(user.services.facebook) {
          user.profile = options.profile; 
          user.profile.picture = "http://graph.facebook.com/" + user.services.facebook.id + "/picture/?type=large";
       }else if(options.profile){
          user.profile = options.profile;
       }
    return user
});

因此,如果某些用户使用 Facebook services 登录,profile.picture 字段将为我们保留带有 Facebook 照片的 url

现在在客户端

JS

    //client
    Template.foo.helpers({
      showProfile:function(){
        if(Meteor.user().profile){
          return userProfile.picture;
        }else{
         return "img/blank_avatar.png"
        }
      }
    })
}

HTML

    {{#if currentUser}}
        <img src="{{showProfile}}" class='fbPic'>
      {{/if}

CSS

.fbPic {
  border-right: 1.5px solid;
  padding-right: 8px;
  padding-bottom: 4px;
  padding-top: 4px;
}