【问题标题】:How To Fix Firebase Realtime Database v9 push method如何修复 Firebase 实时数据库 v9 推送方法
【发布时间】:2022-01-04 14:01:31
【问题描述】:

我已经制作了一个HTML文件test.html,但是提交后没有显示任何数据。 我是不是做错了什么,请帮助我。

我正在使用 Firebase 实时数据库 v9

代码

<html>
    <head>
    <script type="module">
     import { initializeApp } from 'firebase/app';

  import { getDatabase } from "firebase/database";

  const firebaseConfig = {
   apiKey: "*",
   authDomain: "*.firebaseapp.com",
   databaseURL: "https://*.firebaseio.com",
   storageBucket: "*.appspot.com"
  };

  const app = initializeApp(firebaseConfig);

  const database = getDatabase(app);

  import { getDatabase, ref, push } from "firebase/database";

  function writeMessageData() {
      const db = getDatabase();
    
      set(ref(db, 'data/'), {
          name: document.getElementById("feildName").value,
          msg: document.getElementById("feildMessage").value
      });
  }
    </script>
     <title>Test</title>
    </head>
    <body>
        <input type="text" id="feildName" placeholder="Name">
        <input type="text" id="feildMessage" placeholder="Message">
        <button id="btnSubmit" onclick="writeMessageData()">SEND</button>
    </body>
</html>

【问题讨论】:

  • 您可以尝试使函数异步并在set() 之前添加等待吗?当您单击按钮时会发生什么,例如页面刷新或其他什么?
  • 代码乍一看还不错。当您在调试器中单步执行此代码时,问题中的第一行代码没有按照您的预期执行?

标签: javascript html firebase forms firebase-realtime-database


【解决方案1】:

让你的函数异步

async function writeMessageData() {
await set(ref(db,'ThePath'),{
 name: document.getElementById("feildName").value,
 msg: document.getElementById("feildMessage").value
}).then((success) => {console.log(success)}).catch((err) => {console.log(err)})
}
  1. 还要检查您设置的数据库规则。
  2. 尝试删除斜线。将路径从 "data/" 更改为 "data"

大多数情况下它应该会带来好运。

【讨论】:

    猜你喜欢
    • 2023-03-23
    • 2021-10-07
    • 2020-03-25
    • 2021-08-28
    • 2018-04-08
    • 1970-01-01
    • 1970-01-01
    • 2022-07-20
    • 1970-01-01
    相关资源
    最近更新 更多