访问实时数据库时出现问题

我有一个非常简单的网页,我正在尝试使用Firebase进行设置;但由于某种原因,它无法正常工作。

这是index.html代码:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Welcome to Firebase Hosting</title>

    <!-- update the version number as needed -->
    <script defer src="/__/firebase/7.14.4/firebase-app.js"></script>
    <!-- include only the Firebase features as you need -->
    <script defer src="/__/firebase/7.14.4/firebase-auth.js"></script>
    <script defer src="/__/firebase/7.14.4/firebase-database.js"></script>
    ......
    <style media="screen">
    ......
    </style>
  </head>
  <body>
    This is a firebase test page.

    <script src="https://www.gstatic.com/firebasejs/7.14.4/firebase-analytics.js"></script>

    <script>
      // App's Firebase configuration
      var firebaseConfig = {
        apiKey: "A.......A",
        authDomain: "myapp-6638jj.firebaseapp.com",
        databaseURL: "https://myapp-6638jj.firebaseio.com",
        projectId: "myapp-6638jj",
        storageBucket: "myapp-6638jj.appspot.com",
        messagingSenderId: "3.....6",
        appId: "1:....:web:.....",
        measurementId: "G-73RX..XES"
      };
      // Initialize Firebase
      firebase.initializeApp(firebaseConfig);
      firebase.analytics();
    </script>

    <script>
      var dbRef = firebase.database().ref('MyList');
      dbRef.set({"field1": "XX-UUU","field2": "yy-EEE"});
    </script>

  </body>
</html>

此外,出于测试目的,我将数据库访问规则设置为:

{
  "rules": {
    ".read": true,
    ".write": true
  }
}

加载页面后,我希望看到一些数据写入了Realtime数据库,但是它完全是空的。

在Web控制台中,我收到以下错误消息:

ReferenceError: firebase is not defined

对于那些会说的人: 此错误已经有答案,我想指出错误消息不是:

ReferenceError: Firebase is not defined

确实有答案,但不能解决我的问题。

在发表这篇文章之前,我当然尝试了上面代码的一些变体,但没有一个奏效。

有人可以看到(或猜测)我做错了什么吗?

评论
  • nrerum
    nrerum 回复

    在body标签之后添加以下脚本:

    <body>
      <!-- Insert these scripts at the bottom of the HTML, but before you use any Firebase services -->
    
     <!-- Firebase App (the core Firebase SDK) is always required and must be listed first -->
      <script src="https://www.gstatic.com/firebasejs/7.14.4/firebase-app.js"></script>
    

    https://firebase.google.com/docs/web/setup#from-the-cdn

  • 网名已过期
    网名已过期 回复

    如果您收到Firebase未定义的错误,则可能是脚本拼写错误。我建议您检查脚本。

    <script src="https://www.gstatic.com/firebasejs/7.14.2/firebase-app.js"></script> // ıts must be first
    <script src="https://www.gstatic.com/firebasejs/7.14.2/firebase-auth.js"></script>
    <script src="https://www.gstatic.com/firebasejs/7.14.2/firebase-database.js"></script>