Firebase实时数据库:无法从Firebase数据库发送或检索数据

时间:2019-07-31 00:47:16

标签: javascript html

我需要有关实时数据库的帮助:我无法从HTML页面向Firebase发送或检索数据。

HTML:

<head>
</head>

<body>

<script src="https://www.gstatic.com/firebasejs/6.3.3/firebase-app.js"></script>
<script>
var firebaseConfig = {
    apiKey: "AIzaSyAQqSYXa-TRHGQwx_Cz9mpg2dreT38tq6k",
    authDomain: "miparada-e0e6c.firebaseapp.com",
    databaseURL: "https://miparada-e0e6c.firebaseio.com",
    projectId: "miparada-e0e6c",
    storageBucket: "miparada-e0e6c.appspot.com",
    messagingSenderId: "331834990091",
    appId: "1:331834990091:web:a1a7925f63be5faf"
};
 firebase.initializeApp(firebaseConfig);

    function writeData() {
        firebase.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>

我没有得到任何结果。

2 个答案:

答案 0 :(得分:0)

您正在使用Firebase SDK的部分导入,您需要导入实时数据库以及声明的in the documentation,并正确地调用该函数(带有括号)。

<head>
</head>

<body>

    <script src="https://www.gstatic.com/firebasejs/6.3.3/firebase-app.js"></script>
    <!-- import the real time database SDK -->
    <script src="https://www.gstatic.com/firebasejs/6.3.3/firebase-database.js"></script>

    <script>
        var firebaseConfig = {
            apiKey: "AIzaSyAQqSYXa-TRHGQwx_Cz9mpg2dreT38tq6k",
            authDomain: "miparada-e0e6c.firebaseapp.com",
            databaseURL: "https://miparada-e0e6c.firebaseio.com",
            projectId: "miparada-e0e6c",
            storageBucket: "miparada-e0e6c.appspot.com",
            messagingSenderId: "331834990091",
            appId: "1:331834990091:web:a1a7925f63be5faf"
        };
        firebase.initializeApp(firebaseConfig)

        function writeData() {
            firebase.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">
    <!-- call the function with parenthesis -->
    <button onclick="writeData()">SUBMIT</button>

</body>

希望它会有所帮助;)

还:注意安全规则,现在任何人都可以读取/写入数据库中的任何内容。

答案 1 :(得分:0)

在脚本块之后添加这一行代码

<!-- import the real time database SDK -->
<script src="https://www.gstatic.com/firebasejs/6.3.3/firebase-database.js"></script>

因为您没有将实时数据库连接到您的程序