Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

delete node in firebase

been reading docs and other posts but I am unable to remove a node. I am attempting to select the node by a value.

  var eventContactsRef = firebase.database().ref('events-contacts');
  eventContactsRef.orderByChild('eventContactId').equalTo(eventContactId);

then call the remove method on the result

 eventContactsRef.remove(function (error) {
    console.log(error);
  });

nothing happens except a null error value. I am using the latest firebase, most examples are for older versions so I am unsure if I need to get the key and then attempt to delete with that as a reference?

This is the first time using firebase so I am not sure if I have saved the data correctly. here is code to save.

 var key = firebase.database().ref().child('event-contacts').push().key;
  var url = firebase.database().ref('/event-contacts/' + key);
  url.set(eventContacts);

and screenshot

screenshot

like image 693
texas697 Avatar asked Jun 24 '26 18:06

texas697


1 Answers

You cannot remove a query itself. You can only remove the results that match a query.

var eventContactsRef = firebase.database().ref('events-contacts');
var query = eventContactsRef.orderByChild('eventContactId').equalTo(eventContactId);
query.on('child_added', function(snapshot) {
    snapshot.ref.remove();
})
like image 101
Frank van Puffelen Avatar answered Jun 26 '26 15:06

Frank van Puffelen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!