在Meteor中,当尝试访问属性时,我在控制台中得到TypeError:CannotRead属性。但网站正在运行
当尝试读取属性时,meteor给了我一个在Meteor中,当尝试访问属性时,我在控制台中得到TypeError:CannotRead属性。但网站正在运行,meteor,Meteor,当尝试读取属性时,meteor给了我一个TypeError:无法在浏览器控制台中读取未定义的错误的属性'featuredImage'。但它读的是featuredImage,网站运行良好。我怎样才能消除这个错误?这是因为我的订阅还没有准备好吗?是这样吗,怎么解决?(注:我正在使用流量路由器,因此我无法在路由器中等待订阅) 我的模板代码: Template.About.helpers({ page: () => { return findPage(); },
TypeError:无法在浏览器控制台中读取未定义的错误的属性'featuredImage'。但它读的是featuredImage
,网站运行良好。我怎样才能消除这个错误?这是因为我的订阅还没有准备好吗?是这样吗,怎么解决?(注:我正在使用流量路由器,因此我无法在路由器中等待订阅)
我的模板代码:
Template.About.helpers({
page: () => {
return findPage();
},
featuredImage: () => {
var thisPage = findPage();
return Images.findOne({
"_id": thisPage.featuredImage
});
}
});
function findPage() {
return Pages.findOne({
slug: 'about'
});
}
路由器代码:
FlowRouter.route('/about', {
name: 'about',
subscriptions: function() {
this.register('page', Meteor.subscribe('pages', 'about'));
this.register('image', Meteor.subscribe('images'));
},
action() {
BlazeLayout.render('MainLayout', {
content: 'About'
});
setTitle('About Us');
},
fastRender: true
});
订阅可能还没有准备好。FlowRouter提供了一个用于处理此问题的实用程序,您的助手应如下所示:
Template.About.helpers({
page: () => {
// If you only need a specific subscription to be ready
return FlowRouter.subsReady('page') && findPage() || null;
},
featuredImage: () => {
// Ensure ALL subscriptions are ready
if ( FlowRouter.subsReady() ) {
var thisPage = findPage();
return Images.findOne({
"_id": thisPage.featuredImage // Probably should be thisPage.featuredImage._id
});
}
return null;
}
});
但是,为了获得最佳性能,您应该使用if(FlowRouter.subscread('page')&&FlowRouter.subscread('image'))
而不是FlowRouter.subscread()
,因为如果您有其他大的待定订阅,即使您不需要它们,它也会等待它们