gpt4 book ai didi

javascript - 删除 Firebase 数组项并立即自动添加回来(使用 AngularFire)

转载 作者:行者123 更新时间:2023-11-30 12:00:48 24 4
gpt4 key购买 nike

我正在尝试从 $firebaseArray(盒子)中删除一个项目。

删除函数:

function remove(boxJson) {
return boxes.$remove(boxJson);
}

它有效,但它会立即被添加回来:

enter image description here

这是带数组的方法:

function getBoxes(screenIndex) {

var boxesRef = screens
.child("s-" + screenIndex)
.child("boxes");

return $firebaseArray(boxesRef);

}

我想也许我持有对 firebaseArray 的多个引用,当一个删除时,另一个添加,但后来我认为 firebase 应该处理它,不是吗?

不管怎样,我对此一头雾水,有什么想法吗?

更新

当我破解它并删除两次(超时)时,它似乎有效:

function removeForce(screenIndex, boxId) {

setTimeout(function () {
API.removeBox(screenIndex, boxId);
}, 1000);

return API.removeBox(screenIndex, boxId);
}

和 API.removeBox:

function removeBox(screenIndex, boxId) {

var boxRef = screens
.child("s-" + screenIndex)
.child("boxes")
.child(boxId);

return boxRef.remove();
}

最佳答案

当您从 firebase 中删除某些内容时,它是异步的。根据文档,使用 AngularFire 从 firebase 中删除项目的正确方法是:

var obj = $firebaseObject(ref);
obj.$remove().then(function(ref) {
// data has been deleted locally and in the database
}, function(error) {
console.log("Error:", error);
});

$remove() ... Removes the entire object locally and from the database. This method returns a promise that will be fulfilled when the data has been removed from the server. The promise will be resolved with a Firebase reference for the exterminated record.

文档链接:https://www.firebase.com/docs/web/libraries/angular/api.html#angularfire-firebaseobject-remove

关于javascript - 删除 Firebase 数组项并立即自动添加回来(使用 AngularFire),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36659165/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com