我正在尝试从mongodb中的属性设置会话。我在本地有此工作,但是部署后,我会在控制台和白色屏幕上遇到此错误。
DEPS重新计算的例外:TypeError:无法读取属性 未定义的" siteTheme"
// helper
Handlebars.registerHelper("site", function(){
host = headers.get('host');
theSite = Site.findOne({'domain': host});
theme = theSite.siteTheme;
// Problem - Works locally, not deployed with mup.
// Exception from Deps recompute: TypeError: Cannot read property 'siteTheme' of undefined
Session.set("theme", theme);
return theSite;
});
// Add theme class to html
siteTheme0 = function(){
$('html').addClass('theme0');
};
siteTheme1 = function(){
$('html').addClass('theme1');
};
siteTheme2 = function(){
$('html').addClass('theme2');
};
siteTheme3 = function(){
$('html').addClass('theme3');
};
// Change theme on change to db
Deps.autorun(function (c) {
if (Session.equals("theme", "1")){
siteTheme1();
}
else if (Session.equals("theme", "2")){
siteTheme2();
}
else if (Session.equals("theme", "3")){
siteTheme3();
}
else {
Session.set("theme", "0");
siteTheme0();
}
});
这是流星最常见的问题之一。当调用助手(或不存在)时,您的收集数据还没有准备好,因此Site.findOne
返回undefined
,并且您无法访问undefined
的siteTheme
。看到我对这个问题的回答。基本上,您只需要添加某种警告或返回语句,并假设数据可能还没有准备就绪。例如:
Handlebars.registerHelper("site", function(){
var host = headers.get('host');
var theSite = Site.findOne({'domain': host});
if (theSite) {
var theme = theSite.siteTheme;
Session.set("theme", theme);
return theSite;
}
});
如果您的代码的其余部分编写正确,则您的模板应在数据准备就绪后立即渲染。