2016-08-23 56 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

基本上它是从哪里来的火力点的数据是在对象的形式,你不能直接打印对象作为孩子的反应,你必须循环你的对象,然后推入阵列,然后你可以在反应孩子上打印数组值