【问题标题】:Firebase and JavaScript: Update fetched data on website from real time database without reloading website [closed]Firebase 和 JavaScript:在不重新加载网站的情况下从实时数据库更新网站上获取的数据 [关闭]
【发布时间】:2021-02-18 23:08:22
【问题描述】:

我想将网站部署到 Firebase。该网站必须从 firebase 数据库中获取数据。 如果数据库中的值已更新,我希望网站上获取的数据将自动更新,而无需重新加载页面。

您有什么想法,如何在后台自动更新 firebase 网站上的数据? 如果您有任何建议,请告诉我。

【问题讨论】:

    标签: javascript database firebase firebase-realtime-database background


    【解决方案1】:

    这个问题有点含糊,但您将调用数据库并监听 firebase 中数据的更改。如果数据发生变化,下面的方法将重新运行。

    //Get a database reference    
    var exampleRef = firebase.database().ref().child("the_data_to_listen_to");
    
    //.on() will call the data from the database and will listen for changes.
    exampleRef.on("value", function(snapshot){
        //Update the data in the UI here.
        document.getElementById("my_div").innerText = snapshot.val();
    });
    

    现在,每次数据库中的数据发生变化时,它都会通过获取 id 为“my_div”的 div 并从数据库中插入快照的值来更新 UI。您可以创建更复杂的结果、构建表格、更新图表等,但原理是一样的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-09-27
      • 2012-05-26
      • 2013-05-25
      • 1970-01-01
      • 2019-07-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多