【发布时间】:2021-02-18 23:08:22
【问题描述】:
我想将网站部署到 Firebase。该网站必须从 firebase 数据库中获取数据。 如果数据库中的值已更新,我希望网站上获取的数据将自动更新,而无需重新加载页面。
您有什么想法,如何在后台自动更新 firebase 网站上的数据? 如果您有任何建议,请告诉我。
【问题讨论】:
标签: javascript database firebase firebase-realtime-database background
我想将网站部署到 Firebase。该网站必须从 firebase 数据库中获取数据。 如果数据库中的值已更新,我希望网站上获取的数据将自动更新,而无需重新加载页面。
您有什么想法,如何在后台自动更新 firebase 网站上的数据? 如果您有任何建议,请告诉我。
【问题讨论】:
标签: javascript database firebase firebase-realtime-database background
这个问题有点含糊,但您将调用数据库并监听 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。您可以创建更复杂的结果、构建表格、更新图表等,但原理是一样的。
【讨论】: