简体   繁体   English

如何使用HTML5和Javascript读取firebase数据?

[英]How to read a firebase Data using HTML5 and Javascript?

When I want to read the data of my Firebase, nothing appears. 当我想阅读Firebase的数据时,什么都没有显示。

I have tried to read the data using a script for this. 我试图使用脚本来读取数据。

This is my HTML code: 这是我的HTML代码:

<html>
    <head>
        <title>Javascript Firebase</title>
        Humidity: <humidity></humidity><br>
            Clock: <clock></clock><br>
        </head>
        <body>

and this is my script: 

<script type="text/javascript">
        var database = firebase.database('javascript-firebase-6567d');
        var h document.querySelector('humidity');
        var c = document.querySelector('clock');

        var pad = function(x){
        return x < 10 ? '0' +x : x;
        }

        var ShowClock = function() {
        var d = new Date();
        var h = pad(d.getHours());
        var m = pad (d.getMinutes());
        var s = pad (d.getSeconds()); 


        c.innerHTML = [h,m,s].join(':');
        }
        setInterval(ShowClock,1000);

        var myRef = Firebase('https://javascript-firebase-6567d.firebaseio.com/rooms');
        myRef.on('child_changed',function(snapshot){
        data = snapshot.val();
        h.innerHTML = data.humidity;
        });
        </script>

The results that I hope is to see the changes that the child "humidity" shows but currently I can not see the results of the clock or the humidity 我希望的结果是看到孩子“湿度”显示的变化,但目前我看不到时钟或湿度的结果

This code: 这段代码:

 myRef.on('child_changed',function(snapshot){
    data = snapshot.val();
    h.innerHTML = data.humidity;
 });

The child_changed event only fires when the data in the location changes . child_changed事件仅在位置中的数据更改时触发。 If you're trying to show the current child nodes under a location, you'll want to use child_added : 如果您尝试在某个位置下显示当前子节点,则需要使用child_added

 myRef.on('child_added',function(snapshot){
    data = snapshot.val();
    console.log(data);
 });

The child_added fires immediately after attaching the listener for any existing child node, and then subsequently for any new nodes that are added. 在为任何现有子节点附加侦听器之后立即触发child_added ,然后为添加的任何新节点触发。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM