texas697
texas697

Reputation: 6387

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

Upvotes: 3

Views: 7564

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598728

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();
})

Upvotes: 6

Related Questions