I'm using Firebase for a web app. It's written in plain Javascript using no external libraries.
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,
"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."
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 here but 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.
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.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()
takes one parameter a callback function to help deal with this situation to perform operations after we know that the operation has been completed (with or without an error)..push()
takes two params, a value and a callback just like.remove()
.Here is your example code with modifications:
Firebase.remove()
like probably most Firebase methods is asynchronous, thus you have to listen to events to know when something happened: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.remove
method:It's kind of tricky but you can delete a specific node by setting it's data to nothing, for example:
I hope this code will help someone - it is from official Google Firebase documentation:
To remove a record.
In case you are using axios and trying via a service call.
can help.