2013-01-09 29 views
7

Meteor.loginWithPassword()登錄用戶或用Accounts.createUser(兩個客戶端)創建一個新用戶後,我可以在其回調中確認Meteor.user()確實包含所有設置記錄的屬性。Meteor.user()只返回它的_id

{ _id: "XXX", 
    profile: { 
    name: "Joe Shmoe", 
    thumbnail: "http://www.YYY.com/ZZZ.jpg" 
    }, 
    username: "joeshmoe" } 

此外,根據the official docs

默認情況下,當前用戶的用戶名,電子郵件和個人資料發佈到客戶端。

所以,會有人能夠告訴爲什麼當我嘗試在我的模板來訪問這些領域正是如此

Template.login.user_name = function() { 
    return (Meteor.userId() ? Meteor.user().profile.name : '') 
}; 

失敗由於Meteor.user()只返回{_id: "XXX"}與沒有它的實際性能?即用戶肯定已登錄,但用戶對象突然丟失/正在隱藏其所有屬性。

任何人都知道問題可能是什麼?

非常感謝。

編輯:這發生在流星0.5.4,在這個時候的最新版本寫作。接受的答案確實解決了這個問題;有時Meteor.userId()在對象的其餘部分從服務器到達之前已經有效。感謝大家。

+0

我甚至試圖發佈他們在服務器上,並自動訂閱他們在客戶端上,但沒有改變。但我不需要那樣做。 (「userData」,function(){ }返回Meteor.users.find({_ id:this.userId},{fields:{profile:1,username:1}}); //返回Meteor.users.find。 }); ... //客戶 Meteor.autosubscribe(函數(){ Meteor.subscribe( 「用戶數據」); });' – cneuro

+0

然後我把整個呼叫到一個getter方法到Meteor.methods,懷疑模板上下文禁止某些數據庫訪問器的安全性,但這完全是同一個問題。 (){ '//客戶端 流星方法({0} {0} {0}用戶名:函數(){ 返回Meteor.user().profile.name; }); ... Template.login.user_name = function(){ return(Meteor.userId()?Meteor。call(「userName」):'') };' – cneuro

+0

什麼版本的Meteor? –

回答

11

有可能數據尚未從服務器到達。而不是隻檢查Meteor.userId,如果你檢查屬性會發生什麼?

Template.login.user_name = function() { 
    return Meteor.userId() && Meteor.user() && Meteor.user().profile ? Meteor.user().profile.name : ""; 
} 
+0

確實,這似乎是問題所在。我有點懷疑它是良性的,我只是沒有明確檢查對象的其餘部分,因爲我信任'Meteor.userId()'爲null或者與其餘的用戶數據。感謝那。 – cneuro

1

我無法重現這個問題,但如果你有用戶ID,就可以得到完整的數據庫,Meteor.users(即使它應該是已經這樣做了)的所有信息。

Template.login.user_name = function() { 
    return (Meteor.userId() ? Meteor.users.findOne({_id:Meteor.userId()}).profile.name : '') 
} 
+0

是的,我也試過,但是這個調用也返回undefined,就好像用戶在創建時一直在那裏,然後簡單地消失。這可以通過客戶端模擬正確響應(我崇拜Meteor的許多事情之一)這一事實來解釋,然後服務器立即糾正它,尚未寫入記錄,並且僅在隨後的持續嘗試中再次有效。 – cneuro

2

這也發生在我身上,使用loginWithFacebook。我使用這個功能,迄今爲止沒有問題:

var reallyLoggedIn = function() { 
    var user = Meteor.user(); 
    if (!user) return false; 
    else if (!user.profile) return false; 
    else return true; 
}; 
相關問題