我最初有以下数据:
"holidays": {
...
"2016-12-19": {
"stegosaurus": true
},
"2016-12-20": {
"lambeosaurus": true,
"stegosaurus": true
},
"2016-12-21": {
"lambeosaurus": true
},
...
}
通过以下查询,
ref.orderByKey().startAt("2016-12-18")
.endAt("2016-12-18")
.on("child_added", function(snapshot) {
console.log("child_added")
});
ref.orderByKey().startAt("2016-12-18")
.endAt("2016-12-18")
.on("child_removed", function(snapshot) {
console.log("child_removed")
});
ref.orderByKey().startAt("2016-12-18")
.endAt("2016-12-18")
.on("child_changed", function(snapshot) {
console.log("child_changed")
});
ref.orderByKey().startAt("2016-12-18")
.endAt("2016-12-18")
.on("child_moved", function(snapshot) {
console.log("child_moved")
});
在初始添加新节点“2016-12-18”到“假期”节点时,我收到一个 child_added
事件。当节点“2016-12-18”被删除时,我得到一个 child_removed
事件。但是当我添加回“2016-12-18”时,这次我没有收到任何事件。这是一个 Firebase 错误吗?
这是一个重复这个问题的 jsbin: https://jsbin.com/sefaha/6/edit?html,console,output
重现:
1) On reload, Click Update button
2) Dino info is displayed
3) Click Delete button
4) Dino info is removed
5) Click Update button
6) Dino info is not displayed anymore
7) Reload/refresh browser
8) Dino info is displayed
9) Click Delete button
10) Repeat step (1)
请注意,在步骤 (1) 中,有时不会显示恐龙信息。这似乎取决于连接的客户端数量。
最佳答案
在使用深度更新重新添加之前在同一 session 中删除的节点时确实存在错误。它已在 Firebase 数据库服务器中隐藏了很长一段时间,可以使用以下代码片段重现:
var query = ref.orderByKey().startAt("2017-01-13").endAt("2017-01-13");
// attaching this listener makes it work
//ref.on("value", function(s) { console.log("value", s.val() )});
query.on("child_added", function(snapshot) { console.log("child_added"); });
query.on("child_removed", function(snapshot) { console.log("child_removed"); });
query.on("child_changed", function(snapshot) { console.log("child_changed"); });
query.on("child_moved", function(snapshot) { console.log("child_moved"); });
function _update() {
console.log("updating child");
ref.update({'2017-01-13/-KaMFzShw40MkBtMypUg': true });
// using this way of setting the value makes it work
//ref.child('2017-01-13/-KaMFzShw40MkBtMypUg').set(true);
};
function _delete() {
console.log("deleting child");
ref.update({'2017-01-13/-KaMFzShw40MkBtMypUg': null});
};
完整的jsbin:https://jsbin.com/zedojum/edit?html,console,output
您可以通过使用 set()
而不是 update 来直接更新较低级别的 child ,或者通过将监听器附加到位置(而不是查询)来解决该错误。
修复后我会在此处发布说明。
关于javascript - 如果删除节点然后重新添加,Firebase 查询不会推送数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41604815/