2014-01-22 33 views
0

我想从mongodb属性设置会话。我在本地工作,但部署后,我在控制台中得到这个错误,并死亡的白色屏幕。流星Session.set()从mongodb财产助手功能不工作后部署

Exception from Deps recompute: TypeError: Cannot read property 'siteTheme' of undefined

// 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(); 
    } 
}); 
+0

打开mongo控制台并确保其中有实际的数据。如果在''domain':host'没有数据,则不会返回任何数据,这会导致您的错误。为此,在你的项目目录中打开一个终端并输入'meteor mongo'来访问你的mongo shell。 – mjkaufer

+0

不相关,但也许值得指出的是,你的句柄助手函数中的'host','site'和'theme'变量最终将成为全局变量。可能不太好。你应该用'var'作为前缀。 – Sean

回答

2

这是与流星最常遇到的问题之一。当你的助手被调用(或不存在)时,你的收集数据还没有准备好,所以Site.findOne返回undefined,你不能访问undefinedsiteTheme。请参阅我对this question的回答。基本上你只需要添加一些警戒或返回语句,并假设数据可能没有准备好。例如:

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; 
    } 
}); 

如果你的代码的其余部分编写正确,你的模板应再次只要数据准备好渲染。

+0

这正是我所缺少的,等待收藏准备就绪。 – bmx269