登录后,铁路由器路由中未定义Meteor.userId()

时间:2015-04-19 00:59:46

标签: javascript node.js meteor iron-router

当用户首次加载站点而未登录时,登录后,下面定义的路径account将解析为/profile/null。用户必须在路径路径正确之前刷新站点。

this.route('account', {
    template: 'profile',
    path: '/profile/' + Meteor.userId()
})

创建专门用于获取参数Meteor.userId()的路线的原因是因为我使用package要求我定义path名称,即:{{ 1}我不认为可以参数。

改善这条路线的更好方法是什么?

1 个答案:

答案 0 :(得分:1)

路线定义在您的应用启动时发生一次,此时Meteor.userId()仍然未定义,这就是为什么您的代码无效,但整个方法都是错误的,您应该改为定义路线如下:

Router.route("/profile/:_id",{
  name:"profile",
  template:"profile",
  waitOn:function(){
    return Meteor.subscribe("userProfile",this.params._id);
  },
  data:function(){
    var user=Meteor.users.findOne(this.params._id);
    return {
      user:user
    };
  }
});

您在iron:router文档中可能错过的是定义带参数(/path/:param语法)的路由的可能性,并使用此参数来配置路由订阅和数据上下文。

修改

如果您想获得此路线的相应动态路径,可以使用path方法:

HTML

<template name="myTemplate">
  {{> ionTab title="Account" path=accountPath}}
</template>

JS

Template.myTemplate.helpers({
  accountPath:function(){
    return Router.path("profile",Meteor.user());
  }
});