Search code examples
meteoriron-router

Iron router: call to Meteor.user() from router controller returns error


In Iron router path controller I'm trying to load different templates depending on user login status like this:

Router.map(function() {
    this.route('start', {
    path: '/',
    controller: StartController
    });
})

StartController = RouteController.extend({
    template: Meteor.user() ? 'home' : 'landing',
    waitOn: function() {
        return [Meteor.subscribe('something')]
    }
});

However I get exception in console: "...Error: Meteor.userId can only be invoked in method calls. Use this.userId in publish functions..."

I don't really understand what that error means, and what Am I doing wrong here.


Solution

  • The code you've posted is executed on the server. On the server, Meteor.user() can only be executed in methods.

    However, even if this error wouldn't exist, your code wouldn't function as you wish anyway. Meteor.user() ? 'home' : 'landing' will be executed once in the beginning (when the user probably isn't logged in), and won't be re-executed when the user signs in/out.

    Read this in the docs, which is kind of doing what you want.