Javascript 如何删除/移除 Firebase 上的节点
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26537720/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to delete/remove nodes on Firebase
提问by user3771957
I'm using Firebase for a web app. It's written in plain Javascript using no external libraries.
我正在将 Firebase 用于网络应用。它是用纯 Javascript 编写的,没有使用外部库。
I can "push" and retrieve data with '.on("child_added")', but '.remove()' does not work the way it says it should. According to the API,
我可以使用 '.on("child_ added")' 来“推送”和检索数据,但是 '.remove()' 并不能像它所说的那样工作。根据API,
"Firebase.remove() - Remove the data at this Firebase location. Any data at child locations will also be deleted. The effect of the delete will be visible immediately."
“Firebase.remove() - 删除此 Firebase 位置的数据。子位置的所有数据也将被删除。删除的效果将立即可见。”
However, the remove is not occurring immediately; only when the entire script is done running. I need to remove and then use the cleared tree immediately after.
但是,删除不会立即发生。只有当整个脚本完成运行时。我需要立即删除并使用清除的树。
Example code:
示例代码:
ref = new Firebase("myfirebase.com") //works
ref.push({key:val}) //works
ref.on('child_added', function(snapshot){
//do stuff
}); //works
ref.remove()
//does not remove until the entire script/page is done
There is a similar post herebut I am not using Ember libraries, and even so it seems like a workaround for what should be as simple as the API explains it to be.
这里有一个类似的帖子,但我没有使用 Ember 库,即便如此,它似乎是一种解决方法,它应该像 API 解释的那样简单。
回答by Frank van Puffelen
The problem is that you call removeon the root of your Firebase:
问题是您调用removeFirebase 的根目录:
ref = new Firebase("myfirebase.com")
ref.remove();
This will remove the entire Firebase through the API.
这将通过 API 删除整个 Firebase。
You'll typically want to remove specific child nodes under it though, which you do with:
不过,您通常希望删除其下的特定子节点,您可以这样做:
ref.child(key).remove();
回答by Ula
I hope this code will help someone - it is from official Google Firebase documentation:
我希望这段代码对某人有所帮助 - 它来自Google Firebase 官方文档:
var adaRef = firebase.database().ref('users/ada');
adaRef.remove()
.then(function() {
console.log("Remove succeeded.")
})
.catch(function(error) {
console.log("Remove failed: " + error.message)
});
回答by Gopakumar AP
To remove a record.
删除记录。
var db = firebase.database();
var ref = db.ref();
var survey=db.ref(path+'/'+path); //Eg path is company/employee
survey.child(key).remove(); //Eg key is employee id
回答by David
As others have noted the call to .remove()is asynchronous. We should all be aware nothing happens 'instantly', even if it is at the speed of light.
正如其他人所指出的那样,调用.remove()是异步的。我们都应该意识到没有什么是“立即”发生的,即使它以光速发生。
What you mean by 'instantly'is that the next line of code should be able to execute after the call to .remove(). With asynchronous operations the next line may be when the data has been removed, it may not- it is totally down to chance and the amount of time that has elapsed.
您所说的“立即”是指下一行代码应该能够在调用.remove(). 对于异步操作,下一行可能是数据被删除时,也可能不是——这完全取决于机会和已经过去的时间。
.remove()takes one parameter a callback function to help deal with this situation to perform operations after we knowthat the operation has been completed (with or without an error). .push()takes two params, a value and a callback just like .remove().
.remove()在我们知道操作已经完成(有或没有错误)之后,需要一个参数一个回调函数来帮助处理这种情况以执行操作。.push()需要两个参数,一个值和一个回调,就像.remove().
Here is your example code with modifications:
这是您修改后的示例代码:
ref = new Firebase("myfirebase.com")
ref.push({key:val}, function(error){
//do stuff after push completed
});
// deletes all data pushed so far
ref.remove(function(error){
//do stuff after removal
});
回答by szimek
Firebase.remove()like probably most Firebase methods is asynchronous, thus you have to listen to events to know when something happened:
Firebase.remove()可能大多数 Firebase 方法都是异步的,因此您必须监听事件才能知道发生了什么事情:
parent = ref.parent()
parent.on('child_removed', function (snapshot) {
// removed!
})
ref.remove()
According to Firebase docs it should work even if you lose network connection. If you want to know when the change has been actually synchronized with Firebase servers, you can pass a callback function to Firebase.removemethod:
根据 Firebase 文档,即使您失去网络连接,它也应该可以工作。如果您想知道更改何时实际与 Firebase 服务器同步,您可以将回调函数传递给Firebase.remove方法:
ref.remove(function (error) {
if (!error) {
// removed!
}
}
回答by Ved Prakash
In case you are using axios and trying via a service call.
如果您正在使用 axios 并通过服务呼叫进行尝试。
URL: https://react-16-demo.firebaseio.com/
Schema Name: todos
Key: -Lhu8a0uoSRixdmECYPE
axios.delete(`https://react-16-demo.firebaseio.com/todos/-Lhu8a0uoSRixdmECYPE.json`). then();
can help.
可以帮助。
回答by Ido Bar Lev
It's kind of tricky but you can delete a specific node by setting it's data to nothing, for example:
这有点棘手,但您可以通过将特定节点的数据设置为空来删除特定节点,例如:
your_db_node_ref.set({},
(error) => {
if (error) {
res.send("error");
console.log(error);
} else {
res.send("ok");
}
});

