2016-01-23 100 views
2

我目前正在使用鐵路路由器創建一個Meteor應用程序,並且無法返回2個數據上下文。流星 - 返回2套數據與鐵路由器

Router.map(function() { 
    this.route('account', { 
     path: '/account/:accountId', 
    template: 'account', 
    data: function(){ 

    //assigning the data object to self in order to use the 'accountId' route paramter 
    var self = this; 

     var currentRuns = function(){ 
      return CmrRuns.find({accountId: self.params.accountId}); 
     } 

     var currerntAccount = function(){ 
      return CmrAccounts.findOne({accountId: self.params.accountId}); 
     } 

     var current = {runs: currentRuns, account: currentAccount}; 
     return current; 
    } 
}); 

我想要做的是從CmrAccounts集合中獲取帳戶信息,並從CmrRuns集合中運行信息。

我目前正在收到「currentAccount is not defined」錯誤。 我一直堅持這個爲期2天,現在已經不知所措了。

您是否知道我在這裏做錯了什麼,或者在使用鐵路路由器進行路由時是否有另一種方法來獲取2組數據?

+1

我們可以看到你在哪裏訂閱'CmrRuns'和'CmrAccounts'嗎? –

回答

2

首先檢查您的訂閱。你可以在控制檯中運行相同的客戶端查詢並獲得你想要的結果嗎?

其次,我會簡化數據方法。這應該工作:

data: { 
    runs: CmrRuns.find({accountId: this.params.accountId}), 
    account: CmrAccounts.findOne({accountId: this.params.accountId}) 
    } 

不過請注意,.map被棄用:https://github.com/iron-meteor/iron-router/issues/1503這可能是很多問題的根源。

我也高度推薦離開這種方法。看看FlowRouter作爲替代。通過使用您的模板處理您的訂閱和數據,它變得更加整潔,更易於診斷,維護和遷移。此外,它更容易維護路由之間的數據狀態,這在構建複雜的UI時很可能會遇到。

+0

我已經刪除了函數,並且遵循了你的代碼示例,並且一切都很好看。我之前認爲我曾經涉足過這個結構,但顯然不是。感謝您的幫助:) – Aero

+1

您是否更改過Router.map? – pushplaybang

+0

是的,我已經刪除了Router.map。對於我來說,轉換到FlowRouter有點晚,但我將在未來的項目中使用它。再次感謝 – Aero

相關問題