【问题标题】:Firebase - How do I watch a list of objects for change using "on"?Firebase - 如何使用“on”查看对象列表以进行更改?
【发布时间】:2019-06-23 10:59:35
【问题描述】:

如何使用“on”查看对象列表以进行更改?

我有一个配置文件列表,并希望查看此列表以检查是否添加了新对象或更新了配置文件。所以对列表进行任何更改。

当前代码尝试:

var ref = new Firebase("https://markng2.firebaseio.com");          
var refProfiles = ref.child('profiles');

refProfiles.on('value', function(dataSnapshot) {
      //Change has occurred in the list of profiles.
});

这是我的 Firebase 实时数据库的图像:

【问题讨论】:

  • 我已在上面更新以显示我的代码。 @CodeiSir

标签: angular typescript firebase-realtime-database


【解决方案1】:

如果您想知道是否添加了配置文件或更改了特定配置文件,最好收听 Firebase 的 child_ 事件:

  var ref = new Firebase("https://markng2.firebaseio.com");        
  var refProfiles = ref.child('profiles');

  refProfiles.on('child_added', function(dataSnapshot) {
      var profile = dataSnapshot.val();
      console.log('Profile added: ', profile);
  });

  refProfiles.on('child_changed', function(dataSnapshot) {
      var profile = dataSnapshot.val();
      console.log('Profile changed: ', profile);
  });

更多信息请查看Firebase documentation on reading data

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-12
    • 2020-08-01
    • 1970-01-01
    相关资源
    最近更新 更多