如果函数内部无法存储在Firebase中

时间:2018-10-26 20:17:20

标签: javascript html firebase firebase-realtime-database

好吧,我不了解Firebase Web应用到底有什么问题,但是我了解到我需要在javascript代码之前初始化Firebase应用,但是由于某种原因,我试图将任何数据简单地设置为Firebase,问题是,如果我使用单击功能进行设置,那么现在不行了,相反,如果我将其保留在它设置的功能之外。我想知道如何将其真正放入点击功能中。这是我的代码段。

console.log('javascript working!');
var mainText = document.getElementById("mainText");
var submitBtn = document.getElementById("submitBtn");
    var firebaseRef = firebase.database().ref();
    firebaseRef.child("text").set("some value");
    
    
function submitClick() {
    console.log("Test");
}
<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
    <head>
        <title>TODO supply a title</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <script src="https://www.gstatic.com/firebasejs/5.5.6/firebase.js"></script>
        <script>
            // Initialize Firebase
            var config = {
                apiKey: "******************",
                authDomain: "**************",
                databaseURL: "*************",
                projectId: "****************",
                storageBucket: "***************",
                messagingSenderId: "**************"
            };
            firebase.initializeApp(config);
        </script>
        <script src="index.js"></script> 
    </head>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
    <body>
        <script>
            function submitClick() {
                var mainText = document.getElementById("mainText");
                var submitBtn = document.getElementById("submitBtn");
                var firebaseRef = firebase.database().ref();
                firebaseRef.push().set(mainText);
            }
        </script>
        <div class="container">

            <select id="list" onchange="getSelectedValue();">
                <option value="js">JavaScript</option>
                <option value="php">PHP</option>
                <option value="c#">C#</option>
                <option value="java">Java</option>
                <option value="node">Node.js</option>
            </select><br>
            <script>
                function getSelectedValue() {
                    var selectedValue = document.getElementById("list").value;
                    console.log(selectedValue);
                }
            </script>
            <br>
            <hr>
            <textarea placeholder="Digite algo aqui" id="mainText"></textarea>
            <button type="submit" id="submitBtn" onclick="submitClick()">Enviar</button>
        </div>

        <script src="https://www.gstatic.com/firebasejs/5.5.6/firebase.js"></script>
        <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
        <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>


    </body>
</html>

1 个答案:

答案 0 :(得分:0)

在HTML内的commitClick()上,您发送的不是mainText值,而是<textarea />本身。

此外,在JS文件中设置子项text时,在HTML文件中,您只是在没有子节点的情况下推送了子项。