Search code examples
meteoriron-router

Meteor, Meteor.user() returns undefined in iron router's onRun method


I have some router map code something like this

Router.map ->
  @route '/',
    template: '/'
    onRun: ->
      if Meteor.user()?.profile['userType']?
        if Meteor.user()?.profile['userType'] in ['admin', 'semi-admin'] then Router.go 'adminDashBoard'
        else Router.go 'gameDashBoard'
    data: ->
      console.log Meteor.user()

it works well when I used Meteor 0.8.x, but when I upgraded it into 0.9.x 'Meteor.user()' in onRun method started to return 'undefined'. I tried to make an waitOn method to subscribe current user but it processed after onRun() executed, so I move the onRun: Logic to data: method so that it could be worked. (and it works;;)

What I want to know is why does Meteor.user() return undefined in onRun method nevertheless it was logged in. Actually I've seen so many times Meteor.user() returns null or undefined, or sometimes the other collection's return value was null or undefined. So I checked almost every logics to handle in case of null by '?' check logic in coffee script, but I know it would be bad decision.

I think I missed something important information about iron-router, but I can't figure which was missed out. Please let me know if you have some idea about this.

Thanks in advance.

PS. To be sure the issue, I've written some case of Meteor.user() but the undefined answer was received in the other collections sometimes caused by something similar reasons. I want to be sure about this could be happened or just my mistake.

Thanks.


Solution

  • Not sure if this is the case for you but sometimes Meteor.user returns undefined if the server hasnt yet authenticated the user. Have you tried adding a check for Meteor.loggingIn before Meteor.user?