【发布时间】:2017-04-02 15:46:54
【问题描述】:
所以我使用 Google 的 Firebase 构建了一个数据库和一个网络应用程序。数据库和 Web 应用程序都已部署并且都可以工作。我正在尝试使用 HTML 和 javascript 从数据库中读取值。对于我尝试的两种不同方式,在我的网页上显示的值为 null。我附上了保存在数据库中的值的图片。
<script src="https://www.gstatic.com/firebasejs/3.6.1/firebase.js">
</script>
<script>
// Initialize Firebase
var config = {
apiKey: "AIzaYUIYUkokngctrIbR9krB6Do",
authDomain: "HelloWorld.firebaseapp.com",
databaseURL: "https://HelloWorld.firebaseio.com/",
storageBucket: "HelloWorld.appspot.com",
messagingSenderId: "8298539696"
};
//This was the first was we tried to do it
firebase.initializeApp(config);
//var rootRef = firebase.database().ref();
//var dist = rootRef.child("dalemccaugh");
var ref = firebase.database().ref();
ref.once("value")
.then(function(snapshot) {
var test = snapshot.child("dalemccaugh").val();
console.log(test);
});
//The was the second way we tried
function printData() {
//console.log(dist)
dist.on("value", function(snapshot) {
console.log(snapshot.val());
}, function(errorObject) {
console.log("The read failed: " +errorObject.code);
});
}
</script>
【问题讨论】:
-
如果快照值在您的
then或on回调中记录为null,则您正在检查的位置没有值。 -
您检查过控制台是否有错误 - 可能 firebase 没有正确初始化...
标签: javascript html firebase firebase-realtime-database