我正在做一个JavaScript项目,我创建了一个html表并显示了firebase数据库中的表数据,它运行良好,我的问题是当我通过html页面在firebase数据库中添加了一个数据,它成功地存储在firebase数据库中,但是我的html表没有实时更新,它在刷新页面后显示,下面我添加了我的js代码
database.once("value").then(function(snapshot){
if (snapshot.exists()){
snapshot.forEach(function (childSnapshot) {
var childKey = childSnapshot.key;
var childData = childSnapshot.val();
var row = tblUsers.insertRow(rowIndex);
var cellName = row.insertCell(0);
var cellButtons = row.insertCell(1).outerHTML =
"<tr id='row" + rowIndex + "'><td><input type='button' value='Delete' class='delete' onclick=\"delete_row('" + childKey + "')\"></td></tr>";
cellName.appendChild(document.createTextNode(childData.desc));
rowIndex = rowIndex + 1;
document.getElementById("empty_data").style.visibility = "hidden";
});
} else{
document.getElementById("empty_data").style.visibility = "visible";
}
});
null
当您用
这还意味着您不能再使用
因此更改应该限于您共享的代码的第一行,并且如下所示:
database.on("value", function(snapshot){
...