2015-03-02 44 views
0

我正試圖在流星中使用以下內容來調用Facebook個人資料照片。使用幫手來調用Facebook照片METEOR-JS

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

在我的瀏覽器控制檯調用這個,我得到一個錯誤說在模板助手

例外:頭像

我真的不明白是什麼問題...

+0

只是爲了確定真正的問題是在助手上而不是在賬戶上 - facebook console.log 2 return statment並且看起來你得到了什麼,如果你得到了一個正確的url將它放在標籤上內容,順便說一句幫手看起來不錯,你還有另一個名爲「頭像」的模板助手? – Ethaan 2015-03-03 00:01:49

+0

@Ethaan嘿。感謝您的答覆。我仍然無法解決它。我只有一個頭像助手。我試過把它稱爲不同的東西,但沒有成功。另外,'Meteor.user()。services.facebook''似乎沒有在我的應用程序出於某種原因。所以下面的回報聲明... – 2015-03-03 13:13:14

回答

0

讓我們將該Facebook邏輯更改爲server side,即onCreateUser方法。

//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 servicesprofile.picture場將舉行我們url與Facebook的照片。

現在在客戶端

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

我讓你的答案。但是,用我擁有的邏輯來實現它是不可能的? – 2015-03-04 14:21:48

+0

我將''userProfile.picture''改爲''返回Meteor.user().profile.picture;'' – 2015-03-04 14:28:48