简体   繁体   中英

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

Here is the html:

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

Here is the client side js code:

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. But the Meteor.user() turns out to be undefined. I assume that the Meteor.user() result should automatically updated later. so I waited for a while,but it didn't update neither in my chrome browser.

And I know the problem has something to do with the fact that the DOM rendered before the data is ready. 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,

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.users.findOne({_id : this.userId});

Note : Meteor provides a beautiful API for managing User-Accounts

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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