简体   繁体   中英

Undefined Meteor.user() after refresh Angular2 Meteor

I'm quite new to Angular2 and Meteor. I want to do a pretty simple thing : Get the current user information from Meteor with Meteor.user(). The thing is, whenever I refresh the page, the profile value of Meteor.user() becomes "undefined".

I did some search, apparently Meteor.user() is too long to be execute. Apparently, you can use Tracker.autorun() to wait for Meteor.user() to get initialized.

At the moment my code looks like this:

Tracker.autorun(function () {
        Meteor.subscribe("userLoggedIn");
        console.log(Meteor.user());
        if(Meteor.user() !== undefined) {
           //Do something
           this.myFunction();
        }
     });

The thing is that "myFunction" is "undefined" since it's not defined in the Tracker (I think at least).

Do you have any idea how can I call my function ? Is this ok to execute that much of code in the tracker ? Is it possible to wait for the initialization of Meteor.user() outside of the Tracker ?

(Yeah, i've got a lot of question :p )

Thank you by advance ! :)

If you are relying on this , why not using an arrow function to keep the context?

Tracker.autorun(() => { this.myFunction(); });

You could also set a scoped variable, old school way:

var self = this;

Tracker.autorun(function () { /* bla bla */ self.myFunction(); });

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