Firebase和HTML表之间的实时更新

下面的代码显示了从firebase到表的数据

    
Brand Description Item Code Product Category
// Initialize Firebase var config = { apiKey: "AIzaSyB5aqeEmEIB56IvRt4DL8RyOr0JX5Mw3HQ", authDomain: "tp-firebase-fd827.firebaseapp.com", databaseURL: "https://tp-firebase-fd827.firebaseio.com", projectId: "tp-firebase-fd827", storageBucket: "tp-firebase-fd827.appspot.com", messagingSenderId: "191127646247" }; firebase.initializeApp(config); var database = firebase.database(); database.ref().once('value', function(snapshot){ if(snapshot.exists()){ var content = ''; snapshot.forEach(function(data){ var val = data.val(); content +=''; content += '' + val.brand + ''; content += '' + val.description + ''; content += '' + val.item_code + ''; content += '' + val.prod_cat + ''; content += ''; }); $('#ex-table').append(content); } });

但是,当我更改firebase中的数据时,它不会自动更改。 我如何实现实时更新? 这是我唯一需要做的事情。 TYSM将来的帮助

您使用了“一次”方法,该方法一次侦听更改然后停止。 你想使用持续监听变化的“on”方法。 例如,而不是:

database.ref().once('value', function(snapshot){

使用:

database.ref().on('value', function(snapshot){

相关文档: https : //firebase.google.com/docs/database/web/read-and-write#listen_for_value_events