[英]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.