2014-10-11 86 views
0

鐵路由器路徑控制器我試圖加載依賴於用戶登錄狀態像這樣不同的模板:鐵路由器:路由器控制器來電Meteor.user()返回錯誤

Router.map(function() { 
    this.route('start', { 
    path: '/', 
    controller: StartController 
    }); 
}) 

StartController = RouteController.extend({ 
    template: Meteor.user() ? 'home' : 'landing', 
    waitOn: function() { 
     return [Meteor.subscribe('something')] 
    } 
}); 

但是我得到異常在控制檯中: 「...錯誤:只能在方法調用中調用Meteor.userId。在發佈函數中使用this.userId ...」

我真的不明白那個錯誤意味着什麼,我在這裏做錯了。

回答

1

您發佈的代碼在服務器上執行。在服務器上,Meteor.user()只能在方法中執行。

但是,即使此錯誤不存在,您的代碼仍然無法正常工作。 Meteor.user() ? 'home' : 'landing'將在開始時執行一次(當用戶可能未登錄時),並且在用戶登錄/註銷時不會被重新執行。

閱讀this在文檔中,這是做你想做的。