如何正确使用铁路由器waitOn路由选项?

时间:2014-03-23 21:40:54

标签: meteor iron-router

我正在使用iron-router dev 分支,而我在使用waitOn选项时遇到了个人资料路由问题。我的路线/页面是我显示当前登录用户的个人资料信息的地方,例如:

  • 全名/用户名
  • 个人资料图片
  • 描述等。

要获取这些(附加字段),我必须按照Meteor Docs中所述在服务器上创建出版物:

Meteor.publish("userData", function () {
    if (this.userId) {
        return Meteor.users.find({_id: this.userId}, {fields: {services: true}});
    } else {   
        this.ready();
    }
});

现在,当我转到我的个人资料页面时,我可以获得我要求的额外信息(即服务对象),但这并不是如果我留在页面上并刷新我的浏览器就会发生。根据{{​​3}},我认为这与数据的提供时间有关。

我记得我在铁路由器中看到了waitOn选项,所以我尝试使用它,但它并没有改变任何东西。所以我猜测有些东西我误解了。我根据Tom Coleman's comment本书的建议构建了我的代码,这就是我所做的。

server / app.js

Meteor.publish("userData", function () {
    if (this.userId) {
        return Meteor.users.find({_id: this.userId}, {fields: {services: true}});
    } else {   
        this.ready();
    }
});

client / main.js

Meteor.subscribe("userData");

client / views / profile / view.js 中,我创建了以下模板助手,以获取用户的全名。 (我知道它很难看!)

 Template.profile.helpers({
     fullName: function(){
     var currentUser = Meteor.user();       
     if(currentUser.services.google && currentUser.services.google.name)
         return currentUser.services.google.name;
     else if(currentUser.services.facebook && currentUser.services.facebook.name)
         return currentUser.services.facebook.name;
     else
         return currentUser.username;
     }  
  });

lib / router.js

this.route('profile', {
    path: '/profile',
    onAfterAction: function(){
       Session.set("active_link", "profile");
    },
    waitOn: function () {
       return Meteor.subscribe('userData');
    }
 });

添加上面的waitOn选项并没有改变任何内容。这是我在浏览器页面上刷新浏览器时遇到的错误。但是,从其他页面移至个人资料页面会生成此错误。

Exception from Deps recompute function: TypeError: Cannot read property 'google' of undefined
at Object.Template.profile.helpers.fullName (http://localhost:3000/client/views/profile/view.js?cc44954615a9c4eecd3d622d73a56599f483e44a:4:26)
at http://localhost:3000/packages/ui.js?b8fc4eba097393a5ae3f39738758c1a4e16d7b87:2787:23
at Spacebars.call (http://localhost:3000/packages/spacebars.js?dad2d487bcc43e537226e528539ce6389ad6ca4e:167:18)
at Spacebars.mustacheImpl (http://localhost:3000/packages/spacebars.js?dad2d487bcc43e537226e528539ce6389ad6ca4e:104:25)
at Object.Spacebars.mustache (http://localhost:3000/packages/spacebars.js?dad2d487bcc43e537226e528539ce6389ad6ca4e:108:39)
at http://localhost:3000/client/views/profile/template.view.js?7be9db8e2aaaba4e5c4c6e472cf2be15d26dcae1:54:24
at http://localhost:3000/packages/ui.js?b8fc4eba097393a5ae3f39738758c1a4e16d7b87:2286:21
at http://localhost:3000/packages/deps.js?9ff43a2bc56a25afccffa154c66253273407b6e5:347:45
at Object.Meteor._noYieldsAllowed (http://localhost:3000/packages/meteor.js?0f5145b6aeebbdfecdf65c9cb41449149646e99b:551:10)
at null._func (http://localhost:3000/packages/deps.js?9ff43a2bc56a25afccffa154c66253273407b6e5:347:14) 

1 个答案:

答案 0 :(得分:2)

当你点击刷新时,meteor将再次启动登录过程。如果您位于假定用户已登录的网页上,则会出现Deps错误,因为Meteor.user()将返回undefined。以下是您可以尝试的一些事项:

  1. 如果您只删除else出版物中的userData条款会怎样?如果您在用户登录时尝试等待数据,似乎表明发布已准备就绪并不是您想要的。

  2. 使用before hook来捕捉用户未登录的情况,并执行一些有用的操作,例如显示' loading'或者“注册”页面,而不是立即呈现所请求的任何页面。

  3. 为您的fullName助手添加一名警卫(每当您看到Deps错误时,这都是常见做法)。如果用户没有登录,只需返回一个空字符串。一旦用户登录,该函数将再次被评估并返回正确的结果。