您好,我正在尝试创建一个使用Firebass作为后端的网站,但是从数据库中读取数据时遇到了问题。我已经仔细检查了路径,是的,那里有数据。如果有人可以快速查看我在做什么错,我将不胜感激。我没有收到任何错误,它只是无法正常工作。
<html>
<head>
<script src="https://www.gstatic.com/firebasejs/3.5.2/firebase.js"></script>
<script src="https://www.gstatic.com/firebasejs/3.5.2/firebase-app.js"></script>
<script src="https://www.gstatic.com/firebasejs/3.5.2/firebase-auth.js"></script>
<script src="https://www.gstatic.com/firebasejs/3.5.2/firebase-database.js"></script>
<script>
// Initialize Firebase
var config = {
apiKey: "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
authDomain: "my-app.firebaseapp.com",
databaseURL: "https://my-app.firebaseio.com",
storageBucket: "my-ap.appspot.com",
messagingSenderId: "123456789"
};
</script>
<script>
function main(){
firebase.initializeApp(config);
var database = firebase.database();
var ratesRef = database.ref("users/rates");
ratesRef.on('child_added', function(snapshot){
console.log("Working");
console.log("lat:" + snapshot.val().latitude);
});
}
</script>
</head>
<body onload="main();">
hello
</body>
</html>
要从数据库读取,您需要2个回调,其中1个用于读取,1个用于错误。这将为您工作:
firebase.database().ref("users/rates").once('value', readFn(),errFn(errorObject))
或用这个代替main
function main(){
firebase.initializeApp(config);
var database = firebase.database();
var ratesRef = database.ref("users/rates");
ratesRef.on('child_added', function(snapshot){
console.log("Working");
console.log("lat:" + snapshot.val().latitude);
}, function(err) {console.log(err)});
}
您还应该使用一次,以防止在您请求时多次阅读
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句