Javascript Meteor:模板帮助程序中的异常不会消失
我从应用程序的侧栏中得到以下错误 模板帮助程序中出现异常:TypeError:无法读取未定义的属性“org” 在Object.Template.sidebar.helpers.orgUsers()处 在bindDataContext()上 在火焰中。_wrapCatchingExceptions() 在 位于Function.Template.\u with templateInstanceFunc() 在wrapHelper() 在Object.spacebar.call()处 在 在空。() 在 侧边栏应显示特定于组织(组织化)的用户列表。发送用户的助手如下所示:Javascript Meteor:模板帮助程序中的异常不会消失,javascript,meteor,Javascript,Meteor,我从应用程序的侧栏中得到以下错误 模板帮助程序中出现异常:TypeError:无法读取未定义的属性“org” 在Object.Template.sidebar.helpers.orgUsers()处 在bindDataContext()上 在火焰中。_wrapCatchingExceptions() 在 位于Function.Template.\u with templateInstanceFunc() 在wrapHelper() 在Object.spacebar.call()处 在 在空。()
Template.sidebar.helpers({
orgUsers : function() {
var org_id = Meteor.user().org._id;
var users = Meteor.users.find({
"org._id" : org_id
});
return users;
}
});
我在这里发布user.org:
Meteor.publish("userData", function() {
if (this.userId) {
return Meteor.users.find({_id: this.userId},
{fields: {'org._id': 1, 'org.active' : 1, 'emails.[0].address': 1, 'profile.name': 1, 'createdAt':1}});
} else {
this.ready();
}
});
目前我用Iron Router将其发布到每个页面。看起来助手运行了好几次,前几次它没有org\u id或org.\u id,但它最终得到并显示了它。不过,每次刷新页面时,我都会遇到这个严重的控制台错误。非常感谢您的帮助
路由器:
Router.configure({
layoutTemplate : 'layout',
loadingTemplate : 'loading',
notFoundTemplate : 'notFound',
waitOn : function() {
return [
Meteor.subscribe('orgUsers'),
Meteor.subscribe('appointments'),
Meteor.subscribe('allServicesData'),
Meteor.subscribe('allOrgsData'),
Meteor.subscribe('userData'),
];
}
})) 在用户对象可用之前,助手正在运行。您可以在订阅用户数据时使用iron router Wait(推荐方法),也可以为您的助手编写防御代码:
Template.sidebar.helpers({
orgUsers : function() {
if ( Meteor.user() ){
var org_id = Meteor.user().org._id;
var users = Meteor.users.find({
"org._id" : org_id
});
return users;
}
}
});
谢谢你的快速回复!所以订阅已经在等待了。我将编辑问题以显示路由器。我还尝试了防御助手代码,但它仍然存在。您正在执行
Meteor.user().org.\u id
并获得无法读取未定义的属性“org”,这意味着Meteor.user()
在代码运行时不存在。如果你刚刚检查过它,它应该是!