【发布时间】:2020-04-14 22:13:46
【问题描述】:
如果有人能提供帮助,将不胜感激。
我正在尝试从我的 firebase 数据库中接收单个元素并将所述元素放入表中。我已经完成了一次任务,但没有正确保存,我花了很长时间试图重新配置 js
我的目标是从特定团队 ID 中获取用户名,例如 1009254444。
数据库是结构化数据/xbox/3787955/teams/109254444 userName 值在此元素“?”内
https://cfm-stats.firebaseio.com/data.json
我目前的代码是
HTML
<!-- The core Firebase JS SDK is always required and must be listed first -->
<script src="https://www.gstatic.com/firebasejs/7.13.2/firebase.js"></script>
<div class="table-responsive">
<table class="table table-hover table-standings sortable">
<table>
<thead>
<th>Coach</th>
</thead>
<tbody id='userName'></tbody>
</table>
</table>
</div>
css
table {
border-collapse: collapse;
width: 100%;
}
th,
td {
padding: 3px;
text-align: center;
border-bottom: 1px solid #ddd;
}
tr:hover {
background-color: #aa010b;
color: #ffffff;
}
js
// Your web app's Firebase configuration
var firebaseConfig = {
apiKey: "****",
authDomain: "****",
databaseURL: "****",
projectId: "cfm-stats",
storageBucket: "****",
messagingSenderId: "****",
appId: "****",
measurementId: "****"
}
;
firebase.initializeApp(firebaseConfig);
var rootRef = firebase.database().ref('data/xbox/3787955/teams');
var ref = rootRef.child('1009254444');
ref.on('value', function(snap) {
document.getElementById("userName").innerHTML = "";
snap.forEach(function(child) {
var childData = child.val();
var userName = childData.userName;
document.getElementById("userName").innerHTML += "<tr><td> " + userName + "</td></tr>";
});
}
);
【问题讨论】:
-
您在问题中包含了 JSON 树的图片,不幸的是,这并没有为我们提供关于数据路径的大量上下文。请将其替换为实际的 JSON 作为文本,您可以通过单击 your Firebase Database console 的溢出菜单 (⠇) 中的 Export JSON 链接轻松获得。将 JSON 作为文本使其可搜索,让我们可以轻松地使用它来测试您的实际数据并在我们的答案中使用它,一般来说这只是一件好事。
-
我已将图片替换为 json 链接。由于数据库大小,将文本放在这里会很乱。
标签: javascript firebase firebase-realtime-database