2015-12-21 51 views
0

我對建設有一個簡單的應用程序工作的反應,火力地堡 - 一種方法來管理收入和支出,和我的火力地堡設置結構如下:如何使用React訪問Firebase中的嵌套數據?

"cashbook" : { 
    "expenditure" : { 
    "expenditure-1" : { 
     "amount" : "500", 
     "category" : "insurance", 
     "date" : "date", 
     "name" : "Life Insurance", 
     "type" : "recurring" 
    } 
    }, 
    "income" : { 
    "salary" : { 
     "amount" : "500", 
     "category" : "salary", 
     "date" : "date", 
     "type" : "recurring" 
    } 
    } 
} 

我已經反應成立,並與工作完美只是支出,使用Re-base

componentDidMount: function() { 
    // Two way data binding 
    base.syncState('cashbook/expenditure', { 
     context: this, 
     state: 'expenditure' 
    }); 
    }, 

然後我就可以參考使用:this.state.expenditure

然而,當我嘗試展開應用訪問數據的收入,T h變成梨形。我修改了componentdidmount到:

componentDidMount: function() { 
    // Two way data binding 
    base.syncState('cashbook', { 
     context: this, 
     state: 'cashbook' 
    }); 
    }, 

,並嘗試以this.state.cashbook.expenditurethis.state.cashbook.income訪問,但沒有喜悅,我得到的錯誤:Uncaught TypeError: Cannot read property 'expenditure' of undefined

不太確定要嘗試什麼,任何指針都是神聖的棕色。

感謝提前:)

+1

當你嘗試訪問這些東西時會發生什麼?你是否收到特定的錯誤信息?如果您使用React開發人員工具查看組件的狀態,您會看到什麼? –

+0

道歉。更新的主帖,但錯誤是'未捕獲TypeError:無法讀取屬性'開支'未定義' – zeKoko

+1

重新創建基地在這裏。這聽起來像是因爲你的代碼看起來很好,所以它可能是重新編譯的問題。你介意在項目上創建一個問題,我會看看它嗎? –

回答

0

原來我是一個白癡:)

我只是需要在cashbook使用syncstate代替cashbook/expenditure和呼叫this.state.cashbook.expenditure