【发布时间】:2014-03-24 06:16:58
【问题描述】:
我正在使用 iron-router 的 dev 分支,但在为我的 profile 路由使用 waitOn 选项时遇到问题。我的路线/页面是我显示当前登录用户的个人资料信息的地方,例如:
- 全名/用户名
- 个人资料图片
- 描述等
要获得这些(附加字段),我必须按照Meteor Docs 中的说明在服务器上创建一个发布:
Meteor.publish("userData", function () {
if (this.userId) {
return Meteor.users.find({_id: this.userId}, {fields: {services: true}});
} else {
this.ready();
}
});
现在,当我转到我的 个人资料 页面时,我可以获得我请求的额外信息(即 services 对象),但如果我留下来就不会发生这种情况在页面上并刷新我的浏览器。所以我认为这与提供数据的时间有关,根据Tom Coleman's comment。
我记得我在 Iron-router 中看到了 waitOn 选项,所以我尝试使用它,但它并没有改变任何东西。所以我猜我误解了一些东西。我根据Discover Meteor 本书的建议构建了我的代码,这就是我所做的。
在 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)
【问题讨论】:
标签: meteor iron-router