我正在使用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();
}
});
现在,当我转到我的个人资料页面时,我可以获得我请求的额外信息(即服务对象),但如果我留下来,这种情况就不会发生页面上并刷新我的浏览器。所以我认为这与数据可用的时间有关,根据 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 将再次开始登录过程。如果您所在的页面假设用户已登录,您将收到 Deps
错误,因为 Meteor.user()
将返回 undefined
。您可以尝试以下一些内容:
如果您仅删除
userData
发布中的else
子句,会发生什么情况?如果您试图在用户登录时等待数据,那么表明发布已准备就绪似乎并不是您想要的。使用before hook捕获用户未登录的情况并执行一些有用的操作,例如显示“正在加载”或“登录”页面,而不是立即呈现所请求的任何页面。
向您的
fullName
帮助器添加一个防护(每当您看到Deps
错误时,这是常见的做法)。如果用户未登录,只需让它返回一个空字符串。用户登录后,该函数应该再次评估并返回正确的结果。
关于meteor - 如何正确使用iron-router的waitOn路由选项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22597465/