简体   繁体   English

将JSON数据输出到从FireBase数据库提取的道具中

[英]Outputting JSON data into props pulled from FireBase database

I am attempting to output a set of a data from a real-time database into html elements, I have no issues in taking the data from the server and using setState to save the data. 我正在尝试将实时数据库中的一组数据输出到html元素中,从服务器获取数据并使用setState保存数据没有问题。 I have a custom HTML object from a functional Component that will output props, but I am unable to map the object and the inner objects. 我有一个来自功能组件的自定义HTML对象,该组件将输出道具,但是我无法映射该对象和内部对象。

Here is the code responsible for pulling the data from the database: 这是负责从数据库中提取数据的代码:

  componentDidMount() {
    axios.get('./tasks.json')
      .then(response => {
        const fetchedTasks = [];
        for (let key in response.data) {
          fetchedTasks.push({
            ...response.data[key],
            key: key
          });
        }
        this.setState((prevState, props) => {
          return {
            taskList: fetchedTasks
          }
        } )
      })
      .catch(error => console.log(error));
  }

And this is the data I am pulling from the database. 这就是我从数据库中提取的数据。 I want to be able to display each item per it's name, id etc. This is an export of the JSON data from the server, this is the copied into the state of my class component via the axios.get call 'Past.js' 我希望能够显示每个项目的名称,ID等。这是从服务器导出JSON数据,这是通过axios.get调用“ Past.js”复制到类组件状态中的

    {
      "tasks" : {
        "09182018" : {
          "-LMgzJGM78f0BHbPf8cc" : {
            "hours" : 0,
            "id" : "2018-09-18T14:02:25.022Z",
            "minutes" : 0,
            "name" : "sadflkjdsalkf",
            "seconds" : 2,
            "start" : "2018-09-18T14:02:22.508Z"
          },
          "-LMgzaEYe0tcCjpxOuPU" : {
            "hours" : 0,
            "id" : "2018-09-18T14:03:38.635Z",
            "minutes" : 0,
            "name" : "safd",
            "seconds" : 2,
            "start" : "2018-09-18T14:03:36.353Z"
          }
        },
        "09192018" : {
          "-LMm7EoPnNdQLrZ5Rg62" : {
            "hours" : 0,
            "id" : "2018-09-19T13:59:31.361Z",
            "minutes" : 0,
            "name" : "sadf",
            "seconds" : 2,
            "start" : "2018-09-19T13:59:29.281Z",
            "user" : "placeholder"
          }
        },
        "09212018" : {
          "-LMve6ihcRX_uZfvBcaC" : {
            "hours" : 0,
            "id" : "2018-09-21T10:24:06.504Z",
            "minutes" : 0,
            "name" : "sadfsd",
            "seconds" : 2,
            "start" : "2018-09-21T10:24:03.841Z",
            "user" : "placeholder"
          },
          "-LMvnBBAWaHaBiGW5VMK" : {
            "hours" : 0,
            "id" : "2018-09-21T11:03:44.420Z",
            "minutes" : 0,
            "name" : "hello",
            "seconds" : 14,
            "start" : "2018-09-21T11:03:29.802Z",
            "user" : "placeholder"
          }
        }
      }
    }

I have attempted to run .map() calls on the state with no luck, mapping the nested item throws 'array.map is not a function'. 我试图在没有运气的状态下运行.map()调用,映射嵌套项会抛出“ array.map不是函数”。 This is the code I have so far: 这是我到目前为止的代码:

render() {
let outPutItems = [];

if (this.state.loading === false) {
  outPutItems = this.state.taskList.map(array => array.map(item => {
    <CompleteTask
      id={item.id}
      taskName={item.name}/>
  })
)}

return (
  <div className="Past">
    <h1>Past Tasks</h1>
      {outPutItems}
      <button onClick={this.collectTasks}>Log the TaskList!</button>
  </div>
);
}

-UPDATE -UPDATE

The top three are what I am able to use in react and the bottom is using dummy data and is my desired output from the JSON data above which has been applied to the state from the firebase server. 前三名是我能够在react中使用的内容,最底层是使用伪数据,这是我希望从JSON数据获得的输出,上面的JSON数据已应用于Firebase服务器的状态。

top output with map method, bottom using placeholder data from state 顶部输出使用map方法,底部使用状态中的占位符数据

I am able to output the top three shown with this code, all other code above is the same: 我可以输出此代码显示的前三名,以上所有其他代码相同:

      {this.state.taskList.map((outer, index) => (
        <CompleteTask
          taskName={outer.key}
        />
      ) )
    }

This should work for you. 这应该为您工作。 Taken from here . 取自这里 In addition, theres no need to check if loading == false before rendering a list. 此外,在呈现列表之前无需检查是否loading == false If there is nothing in the list to be rendered, nothing will render. 如果列表中没有要渲染的内容,则不会渲染任何内容。 Also, as soon as the list is populated it will reflect in the view. 此外,列表一旦填充,它就会反映在视图中。

return (
    <div className="Past">
      <h1>Past Tasks</h1>
      {this.state.taskList.map((item, index) => (
        <CompleteTask id={item.id} taskName={item.name}/>
      )}
      <button onClick={this.collectTasks}>Log the TaskList!</button>
    </div>
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM