[英]Meteor.userId() returns the value of login user but Meteor.user() is undefined in meteor 1.5
這是html:
<div>
<span id="login-entrance">{{loginEntranceContent}}</span>
</div>
這是客戶端js代碼:
loginEntranceContent(){
if (Meteor.userId()==null) {
return 'Log in/Sign up';
} else {
Meteor.user().emails.address;
};
},
現在,我確定該用戶已登錄,因為當我嘗試Meteor.userId()時,它將返回當前用戶的_id的值。 但是Meteor.user()卻是未定義的。 我認為Meteor.user()結果應在以后自動更新。 所以我等了一段時間,但是在Chrome瀏覽器中它都沒有更新。
而且我知道問題與在數據准備好之前渲染DOM有關。 但是我不知道到底發生了什么。
我已經在論壇上四處尋找答案,雖然有類似的話題,但是沒有一個提供清晰的解釋和簡單的解決方案。 因此,我希望為此開辟一個新的話題。
僅供參考,為解決此問題,我嘗試在服務器端制定一種方法:
Meteor.users.findOne({_id:'this.userId'});
並從客戶端調用它 它仍然會得到不確定的結果。
您正在使jsut成為null
檢查,而其他“ falsy ”值檢查又如何呢?您必須正確使用以下代碼,
loginEntranceContent(){
if (!Meteor.userId()) {
return 'Log in/Sign up';
} else if(Meteor.user() && Meteor.user().emails && Meteor.user().emails[0].address){
return Meteor.user().emails[0].address;
} else {
//do something here.
};
},
並且您還沒有向我們展示從客戶端調用Meteor方法的代碼,如@Jankapunkt建議在服務器端使用以下代碼,
Meteor.users.findOne({_id : this.userId});
注意 :Meteor提供了一個漂亮的API,用於管理用戶帳戶
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.