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?
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());
}
}
});