2014-01-16 36 views
0

當我返回模板助手內部不是靜態的東西時,Meteor拋出一個錯誤「Deps重新計算:用戶名異常」。流星投擲「Deps重新計算的例外」

Router.configure({ 
    layoutTemplate: 'layout' 
}); 

Router.map(function() { 
    this.route('home', {path: '/'}); 
    this.route('dashboard'); 
}) 

if (Meteor.isClient) { 
    Accounts.ui.config({ 
    passwordSignupFields: 'USERNAME_AND_EMAIL' 
    }); 

    Template.dashboard.helpers({ 
    username: function() { 
     return Meteor.user().username; 
    } 
    }); 
} 


<template name="layout"> 
    <h1>Layout</h1> 
    {{yield}} 
</template> 

<template name="home"> 
    {{#link route='dashboard'}}Dashboard{{/link}} 
</template> 

<template name="dashboard"> 
    {{#link route='home'}}Home{{/link}} 
    {{username}} 
</template> 

奇怪的是,通過點擊在家裏模板中的鏈接訪問儀表板路由時,一切工作正常。但是當我在url欄中鍵入/儀表板時,我得到了那個錯誤。

回答

1

當您手動導航到一個URL時,它強制meteor重新運行登錄過程。所以,雖然這樣做,Meteor.user()不會返回一個有效的對象(因此你不能訪問username)。你有兩個選擇:

添加後衛你的代碼,如:

Meteor.user() && Meteor.user().username; 

或有你的路由器顯示頁面中的「登錄」,而在登錄過程正在進行中。

+0

謝謝:)如何讓我的路由器等待登錄過程運行? – zimt28

+0

我還沒有從流星路由器遷移到鐵路路由器,所以我不能告訴你我爲此做了什麼。我實際上有點驚訝它不在文檔中,因爲這是一個常見的用例。看看[這個](https://github.com/EventedMind/iron-router/issues/286)問題的一些建議。 –