Meteor,Meteor.user()在铁路由器的onRun方法中返回undefined

时间:2014-10-12 15:50:00

标签: meteor iron-router

我有一些像这样的路由器地图代码

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()

当我使用Meteor 0.8.x时效果很好,但当我将其升级到0.9.x时,onRun方法中的Meteor.user()'开始返回'undefined'。 我尝试使用waitOn方法订阅当前用户,但是在执行onRun()之后处理它,所以我将onRun:Logic移动到data:方法以便它可以工作。 (它有效;;)

我想知道的是为什么Meteor.user()在onRun方法中返回undefined但它已登录。 实际上我已经看过很多次Meteor.user()返回null或undefined,或者有时其他集合的返回值为null或undefined。 所以我检查几乎所有的逻辑来处理null的情况下'?'检查咖啡脚本中的逻辑,但我知道这将是一个糟糕的决定。

我想我错过了一些有关铁路由器的重要信息,但我无法确定哪些错过了。 如果你对此有所了解,请告诉我。

提前致谢。

PS。 为了确定这个问题, 我写了一些Meteor.user()的案例,但是其他集合中收到的未定义答案有时是由类似的原因引起的。 我想确定这可能发生,或者只是我的错误。

感谢。

2 个答案:

答案 0 :(得分:1)

不确定是否适合您,但有时Meteor.user会在服务器尚未对用户进行身份验证时返回undefined。您是否尝试在Meteor.user之前添加Meteor.loggingIn检查?

答案 1 :(得分:0)

我有一个非常类似的问题,但在onBeforeAction:选项上。我通过知道未准备好Meteor.user() 未定义并且 null (如果已准备好但未记录)来修复它。

基本上,就你的例子而言:

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

您可以使用@pause()之类的内容替换@render 'loading'以显示您的加载屏幕,但我认为它不会与onRun:一起使用,因为它只会加载一次{}官方文件(与onBeforeAction:不同)。

相关问题