torayeff
torayeff

Reputation: 9702

Meteorjs iron:router data

I have this route:

Router.route('/', {
    name: 'index',
    template: 'index',
    data: function () {
        console.log(Meteor.user());
    }
});

In browser console, first it displays:

null

and right after:

Object {_id: "aSeHqtE8o7C3x5NsW", profile: Object, username: "torayeff"}

Can anyone explain the reason for double output?

Upvotes: 0

Views: 61

Answers (2)

Jeremy S.
Jeremy S.

Reputation: 4659

The data source is reactive so the function is being re-run as the data source changes. When you first visit the route your Meteor.user() is null; once the user data has arrived the function is run again and the user info is logged to the console.

This is the expected behavior. If you only want to log the output once the data has arrived you can do something like the following:

Router.route('/', {
    data: function () {
        if (Meteor.user()){
          console.log(Meteor.user());
        }
    }
});

Upvotes: 3

ilrein
ilrein

Reputation: 3923

Are you calling {{this}} in a template?

That might explain a double console.log.

Upvotes: 0

Related Questions