简体   繁体   English

在Javascript / HTML中将Firebase数据显示为数组

[英]Displaying Firebase data as array in Javascript/HTML

Edit: Object.keys(snapshot.val())[0] returns "-LfL-eVLT1QtZw-LAvpS" which is the randomly assigned Firebase name. 编辑:Object.keys(snapshot.val())[0]返回“-LfL-eVLT1QtZw-LAvpS”,这是随机分配的Firebase名称。 But how do I display the "text" property of that? 但是如何显示该文本的“文本”属性?

The code below works pretty well. 下面的代码效果很好。 I can hit the Submit Data button and it'll push the data to the Firebase, but I'm having trouble displaying it. 我可以点击“提交数据”按钮,它会将数据推送到Firebase,但我无法显示它。 I want to display the data in an HTML list, but Firebase returns a nested object, not an array, and I don't know what to do with it. 我想在HTML列表中显示数据,但Firebase返回一个嵌套对象,而不是数组,我不知道如何处理它。

I've attached a screenshot so it's clear what my code is doing. 我附上了截图,因此很清楚我的代码在做什么。 JSON.stringify is very promising - it shows all the data, so I know it's actually there and communicating properly with the database. JSON.stringify非常有前途 - 它显示了所有数据,因此我知道它实际上存在并与数据库正确通信。 But it's obviously not usable in any sense. 但它显然在任何意义上都无法使用。 I need to know what I can do so I can apply a .map() to part of my code and make it work, but I'm coming up empty after hours of trying things. 我需要知道我能做什么,所以我可以将.map()应用到我的部分代码中并使其正常工作,但是经过几个小时的尝试后我才会空洞。

Object.keys(snapshot) I thought would convert the object into an array, but I can't tell what that's doing at all. Object.keys(快照)我以为会把对象转换成数组,但我不知道它在做什么。

代码结果

import React, {useState} from 'react'
import fire from './fire'
import './App.css'

const App = () => {
  let [messageList, setMessageList] = useState(fire.database().ref('messages'))

  const handleSubmit = (e) => {
    e.preventDefault()
    //fire.database().ref('messages').push( document.querySelector("#message-input").value );
    fire.database().ref('messages').push({ text: document.querySelector("#message-input").value, id: document.querySelector("#message-input").value })
  }

  var ref = fire.database().ref("messages")
  var fireArray = [fire.database().ref("messages")]

  ref.on("value", function(snapshot) {
    document.querySelector("#value").innerHTML = JSON.stringify(snapshot)
    document.querySelector("#snapshotval").innerHTML = snapshot.val().text
    document.querySelector("#snapshot").innerHTML = snapshot
    document.querySelector("#messages").innerHTML = Object.keys(snapshot)
  }, function (error) {
    console.log("Error: " + error.code);
  });

  return(
    <section>
    <form onSubmit={handleSubmit}>
      <input type="text" id="message-input"></input>
      <button>Submit Data</button>
    </form>
    <h2>JSON.stringify(snapshot)</h2>
    <p id="value"></p>
    <h2>snapshot.val()</h2>
    <p id="snapshotval"></p>
    <h2>snapshot</h2>
    <p id="snapshot"></p>
    <h2>Object.keys(snapshot)</h2>
    <p id="messages"></p>
    </section>
  )
}

export default App;

You can use JSX to display data: 您可以使用JSX显示数据:

<p id="value">{JSON.stringify(messageList)}</p>

messageList would be the result of the Firebase response: messageList将是Firebase响应的结果:

ref.on("value", function(snapshot) {
    setMessageList(snapshot)
  }, function (error) {
    console.log("Error: " + error.code);
  });

More information: https://reactjs.org/docs/introducing-jsx.html 更多信息: https//reactjs.org/docs/introducing-jsx.html

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

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