添加了Firebase JS子项已更改并删除

时间:2017-06-13 13:19:06

标签: javascript firebase firebase-realtime-database

我有child_added功能但在删除或更改内容时不会自动更新。 如何在一个函数中添加删除和更改的子项?

由于

2 个答案:

答案 0 :(得分:0)

你有没有尝试过" child_removed"事件?为四个事件中的每个事件使用事件监听器是正常的;添加,更改,移动和删除,而不是一个组合。

https://firebase.google.com/docs/database/admin/retrieve-data#child-removed

答案 1 :(得分:0)

听起来你正在寻找这样的东西:

function handleEvent(event, snapshot, optionalPreviousChildKey) {
  switch (event) {
    case "child_added": ...
      break;
    ...
  }
}
ref.on("child_added", function(snapshot, previousChildKey) { handleEvent("child_added", snapshot, previousChildKey); })
ref.on("child_changed", function(snapshot, previousChildKey) { handleEvent("child_changed", snapshot); })
ref.on("child_moved", function(snapshot, previousChildKey) { handleEvent("child_moved", snapshot, previousChildKey); })
ref.on("child_removed", function(snapshot) { handleEvent("child_removed", snapshot); })