为什么我的网站界面无法连接到Firebase实时数据库?

时间:2019-06-18 03:27:08

标签: javascript html firebase firebase-realtime-database

我尝试创建一个简单的界面来检查是否可以连接到Firebase实时数据库。但不幸的是,它无法正常工作。

<html>

<body>
  <script src="https://www.gstatic.com/firebasejs/6.2.0/firebase-app.js"></script>
  <script>
    var firebaseConfig = {
      apiKey: "AIzaSyCYidiTYtEPYlO4p1tpCrdPV3Ed726s4TY",
      authDomain: "test-4567f.firebaseapp.com",
      databaseURL: "https://test-4567f.firebaseio.com",
      projectId: "test-4567f",
      storageBucket: "test-4567f.appspot.com",
      messagingSenderId: "683153942498",
      appId: "1:683153942498:web:b6b713ebf4db0658"
    };
    firebase.initializeApp(firebaseConfig);

    function writeData() {
      firebase.database.ref("User").set({
        name: document.getElementById("nameField").value,
        age: document.getElementById("ageField").value
      });
      getData();
    }

    function getData() {
      firebase.database().ref('/').once('value', function(snapshot) {
        snapshot.forEach(function(childSnapshot) {
          var childKey = childSnapshot.key;
          var childData = childSnapshot.val();
          document.getElementById("data").innerHTML = childData['name'] + ", "
          childData['age'];
        })
      })
    }
  </script>
  <h3>Website</h3>
  <br/>
  <input type="text" placeholder="name" id="nameField">
  <input type="text" placeholder="age" id="ageField">
  <button onclick="writeData()">submit</button>
  <p id="data"></p>
</body>

</html>

我在firebase中的数据为空,我不知道我的简单代码有什么问题。

显示错误

enter image description here

0 个答案:

没有答案