简体   繁体   English

Meteor.userId()返回登录用户的值,但Meteor.user()在流星1.5中未定义

[英]Meteor.userId() returns the value of login user but Meteor.user() is undefined in meteor 1.5

Here is the html: 这是html:

<div>
    <span id="login-entrance">{{loginEntranceContent}}</span>
</div>

Here is the client side js code: 这是客户端js代码:

loginEntranceContent(){
    if (Meteor.userId()==null) {
        return 'Log in/Sign up';
    } else {
            Meteor.user().emails.address;
    };
},

Now I am sure that the user is logged in because when I tried Meteor.userId(),it returns the value of the _id of current user. 现在,我确定该用户已登录,因为当我尝试Meteor.userId()时,它将返回当前用户的_id的值。 But the Meteor.user() turns out to be undefined. 但是Meteor.user()却是未定义的。 I assume that the Meteor.user() result should automatically updated later. 我认为Meteor.user()结果应在以后自动更新。 so I waited for a while,but it didn't update neither in my chrome browser. 所以我等了一段时间,但是在Chrome浏览器中它都没有更新。

And I know the problem has something to do with the fact that the DOM rendered before the data is ready. 而且我知道问题与在数据准备好之前渲染DOM有关。 But I don't know what exactly is going on there. 但是我不知道到底发生了什么。

I already went around the forum for answers, there were some similar topic but none had provided a clear explanation and simple solution for it. 我已经在论坛上四处寻找答案,虽然有类似的话题,但是没有一个提供清晰的解释和简单的解决方案。 So I hope it is worthwhile to open a new topic for it. 因此,我希望为此开辟一个新的话题。

FYI, to solve it ,I tried to make a method on server side : 仅供参考,为解决此问题,我尝试在服务器端制定一种方法:

Meteor.users.findOne({_id:'this.userId'});

and called it from the client side. 并从客户端调用它 it still get the undefined result.... 它仍然会得到不确定的结果。

You are making jsut a null check, what about other ' falsy ' value checks, you must properly use the code as below, 您正在使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.
    };
},

and as you have not shown us the code to call Meteor method from client, as @Jankapunkt suggested use code as below at server end, 并且您还没有向我们展示从客户端调用Meteor方法的代码,如@Jankapunkt建议在服务器端使用以下代码,

Meteor.users.findOne({_id : this.userId});

Note : Meteor provides a beautiful API for managing User-Accounts 注意 :Meteor提供了一个漂亮的API,用于管理用户帐户

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM