How to delete/remove nodes on Firebase

后端 未结 6 1391
悲&欢浪女
悲&欢浪女 2020-12-07 22:27

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

相关标签:
6条回答
  • 2020-12-07 22:39

    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
    
    0 讨论(0)
  • 2020-12-07 22:43

    In case you are using axios and trying via a service call.

    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.

    0 讨论(0)
  • 2020-12-07 22:47

    The problem is that you call remove on the root of your Firebase:

    ref = new Firebase("myfirebase.com")
    ref.remove();
    

    This will remove the entire Firebase through the API.

    You'll typically want to remove specific child nodes under it though, which you do with:

    ref.child(key).remove();
    
    0 讨论(0)
  • 2020-12-07 22:51

    I hope this code will help someone - it is from official Google Firebase documentation:

    var adaRef = firebase.database().ref('users/ada');
    adaRef.remove()
      .then(function() {
        console.log("Remove succeeded.")
      })
      .catch(function(error) {
        console.log("Remove failed: " + error.message)
      });
    
    0 讨论(0)
  • 2020-12-07 22:53

    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:

    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
    });
    
    0 讨论(0)
  • 2020-12-07 23:04

    Firebase.remove() like probably most Firebase methods is asynchronous, thus you have to listen to events to know when something happened:

    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.remove method:

    ref.remove(function (error) {
        if (!error) {
            // removed!
        }
    }
    
    0 讨论(0)
提交回复
热议问题