2016-08-23 28 views
0

我正嘗試從Firebase數據庫加載我的初始數據。目前,我可以用數據庫中的一個條目的內容來更新狀態。然而,我想要更多地控制通過數組呈現的內容,但是如何將個案綁定到數組並在迭代中迭代這些事件,但是隻有在執行componentDidMount()之後才能進行迭代。從數據庫陣列中提取和更新視圖

這是我收到的錯誤:

firebase.js:283 Uncaught Invariant Violation: Objects are not valid as a React child (found: object with keys {home}). If you meant to render a collection of children, use an array instead or wrap the object using createFragment(object) from the React add-ons. Check the render method of `Cases`. 

這是我的代碼:

import React, { Component } from 'react'; 
import * as firebase from 'firebase'; 

class Cases extends Component{ 

    constructor() { 
    super(); 
    this.state = ({ 
     cases: 10 
    }); 
    } 

    componentDidMount() { 

     const rootRef = firebase.database().ref('cases'); 
     const casesRef = rootRef.child('home'); 
     rootRef.on('value', snap => { 
     this.setState({ 
      cases: snap.val() 

     }) 
     }) 
    } 

    render() { 
    return (
    <div> 
      <h2>Cases</h2> 
      {this.state.cases} 
     </div>) 
    } 
} 

export default Cases; 
+0

你可以發送什麼是你的this.state.cases的內容? –

+0

嗨謝謝,謝謝你花時間!我調試了一堆,發現了更多的信息。不過,我提出了一個新的問題來更清楚地闡述問題。如果你想看看我上傳的所有信息到這個線程:[鏈接](http://stackoverflow.com/questions/39114285/rendering-javascript-object-in-react) – dwigt

回答

0

基本上它是從哪裏來的火力點的數據是在對象的形式,你不能直接打印對象作爲孩子的反應,你必須循環你的對象,然後推入陣列,然後你可以在反應孩子上打印數組值