Meteor 如何正确使用铁制路由器';什么是waitOn路线选择?
我正在使用的dev分支,在使用我的配置文件的waitOn选项时遇到问题。我的路线/页面显示当前登录用户的配置文件信息,例如:Meteor 如何正确使用铁制路由器';什么是waitOn路线选择?,meteor,iron-router,Meteor,Iron Router,我正在使用的dev分支,在使用我的配置文件的waitOn选项时遇到问题。我的路线/页面显示当前登录用户的配置文件信息,例如: 全名/用户名 侧面图 描述等 要获取这些(附加字段),我必须在服务器上创建一个发布,如中所述: 现在,当我被发送到我的个人资料页面时,我可以获得我请求的额外信息(即服务对象),但如果我停留在页面上并刷新浏览器,就不会发生这种情况。因此,我认为这与数据发布的时间有关 我记得我在iron router中看到了waitOn选项,所以我尝试使用它,但它没有改变任何东西。所以我
- 全名/用户名
- 侧面图
- 描述等
waitOn
选项,所以我尝试使用它,但它没有改变任何东西。所以我猜我误解了什么。我已经根据书中的建议构建了我的代码,下面是我所做的
在服务器/app.js中
Meteor.publish("userData", function () {
if (this.userId) {
return Meteor.users.find({_id: this.userId}, {fields: {services: true}});
} else {
this.ready();
}
});
Meteor.subscribe("userData");
this.route('profile', {
path: '/profile',
onAfterAction: function(){
Session.set("active_link", "profile");
},
waitOn: function () {
return Meteor.subscribe('userData');
}
});
在客户端/main.js中
Meteor.publish("userData", function () {
if (this.userId) {
return Meteor.users.find({_id: this.userId}, {fields: {services: true}});
} else {
this.ready();
}
});
Meteor.subscribe("userData");
this.route('profile', {
path: '/profile',
onAfterAction: function(){
Session.set("active_link", "profile");
},
waitOn: function () {
return Meteor.subscribe('userData');
}
});
在client/views/profile/view.js中,我创建了以下模板帮助器,以获取用户的全名。(我知道这很难看!)
在lib/router.js中
Meteor.publish("userData", function () {
if (this.userId) {
return Meteor.users.find({_id: this.userId}, {fields: {services: true}});
} else {
this.ready();
}
});
Meteor.subscribe("userData");
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()
将返回未定义的。以下是一些你可以尝试的东西:
如果只删除userData
出版物中的else
子句,会发生什么情况?如果您试图在用户登录时等待数据,那么表明发布已准备就绪似乎并不是您想要的
使用来捕获用户未登录的情况,并执行一些有用的操作,如显示“加载”或“登录”页面,而不是立即呈现所请求的任何页面
为您的fullName
助手添加一个保护(每当您看到Deps
错误时,这是常见的做法)。只要让它在用户未登录时返回一个空字符串。用户登录后,应再次评估该函数并返回正确的结果
谢谢你的帮助@David,我按照上面列出的顺序尝试了你的建议,但结果证明#3是正确的选择。我刚刚检查了currentUser.services
是否可用,如果不只是返回currentUser.username
,它在Meteor.user()
中随时可用#1我尝试删除this.ready()
部分,但没有任何更改#2我已经有了一个before钩子,可以将用户重定向到带有登录链接的公共页面,所以这也没用。但是#3是为我做的。谢谢