【问题标题】:Why does firebase.database() throw an error?为什么 firebase.database() 会抛出错误?
【发布时间】:2020-03-14 10:49:05
【问题描述】:

我尝试制作一个使用 firebase 数据库的简单网络应用,但按照教程操作,尝试let database = firebase.database() 时出错。

错误表明firebase.database is not a function

我也试过删除变量并将firebase.database()直接放入我需要调用它的地方,但这也会导致同样的错误。

尽管查看了 firebase 文档,但我还没有在网上找到任何其他方法来解决此问题。

我该如何解决这个问题?谢谢!

我的代码(删除了一些细节):

<html>

    <head></head>

    <body>

        <!-- The core Firebase JS SDK is always required and must be listed first -->
        <script src="https://www.gstatic.com/firebasejs/7.11.0/firebase-app.js"></script>

        <!-- TODO: Add SDKs for Firebase products that you want to use
     https://firebase.google.com/docs/web/setup#available-libraries -->
        <script src="https://www.gstatic.com/firebasejs/7.11.0/firebase-analytics.js"></script>

        <script>
        // Your web app's Firebase configuration
        var firebaseConfig = {
            apiKey: "AIza***********************************",
            authDomain: "databasetest-db3.firebaseapp.com",
            databaseURL: "https://databasetest-db3.firebaseio.com",
            projectId: "databasetest-db3",
            storageBucket: "databasetest-db3.appspot.com",
            messagingSenderId: "9472********",
            appId: "1:947************************************",
            measurementId: "G-EZ9*******"
        };

        // Initialise Firebase
        firebase.initializeApp(firebaseConfig);
        firebase.analytics();
        let database = firebase.database()

        function writeData() {
            database.ref("User").set({
                name: document.getElementById("nameField").value,
                age: document.getElementById("ageField").value,
            })
        }
        </script>
        <h1>User Database</h1>
        <input type="text" placeholder="name" id="nameField">
        <input type="text" placeholder="age" id="ageField">
        <button onclick="writeData()">Submit</button>

    </body>

</html>

编辑:复制和粘贴代码而不是图像

【问题讨论】:

  • 请添加代码示例而不是图片
  • 对不起,我是新手,我不明白你的意思。你能改写一下吗?谢谢!

标签: javascript firebase firebase-realtime-database


【解决方案1】:

由于您使用的是实时数据库,因此您需要添加以下脚本才能使用 sdk:

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

请参阅相应的doc 了解更多详细信息,特别是为什么仅添加 Firebase 核心库(即firebase-app.js)是不够的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-12-10
    • 2019-06-13
    • 2021-06-17
    • 2015-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多