如何在表中附加Firebase数据

时间:2018-09-29 18:04:06

标签: javascript html firebase firebase-realtime-database

Firebase示例:

用户:

  • 用户1

    • A

      • abc
      • def
      • ghi
    • B

      • abc
      • def
      • ghi
    • C
      • abc
      • def
      • ghi

我想显示“ abc”,“ def”,“ ghi”值。但是我的代码仅显示“ abc”。下面是我的代码。

<html>
<body onload="initialize()">

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

<script src="test.js" ></script>
<div class="Medbox">


        <p>Search User
        <input type="text"  id="User_Field" placeholder="Enter User">
        <input type="button" value="Search" onclick="searchValue()">
        </p>

        <p>Current Values: </p>
        <table id="abcTable" class="abcTable">
        <thead>
            <tr>
            <td><b>InfoA</b></td>
            <td><b>InfoB</b></td>
            <td><b>InfoC</b></td>
            </tr>
        </thead>
        <tbody id="TableBody">
        </tbody>    
        </table>

</div>
</html>

我的Javascript:

function initialize(){
// Initialize Firebase
var config = {
apiKey: "AIzaSyB6eYZBKDsasdasddsamjxj2t2k4TI",
authDomain: "medimate-ead90.firebaseapp.com",
databaseURL: "https://asdasd.firebaseio.com",
projectId: "asdas",
storageBucket: "mdasdasd.appspot.com",
messagingSenderId: "5256151964"
};
firebase.initializeApp(config);
database = firebase.database();
}

function SearchValue(){

var searchedUser = User_Field.value;
var ref = firebase.database().ref("User/" +searchedUser);
ref.once('value', function(snapshot) {
snapshot.forEach(function(childSnapshot) {
var childKey = childSnapshot.key;
var childData = childSnapshot.val();

let el = document.querySelector("#TableBody");
el.innerHTML = childKey;
document.appendChild(el);
 });
});
} 

我想让代码读取是否存在“ B”或“ C”,它们将在另一行显示“ abc”,“ def”,“ ghi”值。我是一名学生,刚接触Firebase,请提供帮助或建议。谢谢

0 个答案:

没有答案