Can't Delete Node of Firebase Database

I call the function to delete as follows:

<div *ngFor="let out of cart_checkouts">
<button type="button" (click)="RemoveCheckoutRecord(out.checkoutID)">Delete</button>
</div>

This executes the following function:

RemoveCheckoutRecord(rowID) {
this.firestore.doc('cart_checkouts/' + rowID).delete();
}

I’ve also tried:

RemoveCheckoutRecord(rowID) {
var db = firebase.database();
var ref = db.ref("cart_checkouts");
ref.child(rowID).remove();
}

Neither one of the functions throw an error; they simply don’t work. The preceding two functions seem to just be for collections and this is a node on a realtime database not a collection.

The following works to delete the node -MRGk9sayjVDB8F15TM3 but I need it to delete by checkoutID because I can’t reference the nodes by their keys.

var db = firebase.database();
var ref = db.ref("cart_checkouts/-MRGk9sayjVDB8F15TM3");
ref.remove();

I’m trying to get my realtime database keys with the following code:


constructor(private db: AngularFireDatabase,){}

RemoveCheckoutRecord() {
this.db.list('cart_checkouts').valueChanges()
.subscribe(result => {
for (const res of result){
console.log("result key " + result.payload.key);
}
});
}

I get the error: Property ‘payload’ does not exist on type ‘unknown’.

How can I fix this?

Hey there,

I have no experience with Angular. So, if the issue is there, I will not find it. Also, I might be behind what you are doing, but to debug, it might be helpful to collect the promise from the .remove method:

ref.remove()
  .then(function() {
    console.log("Remove succeeded.")
  })
  .catch(function(error) {
    console.log("Remove failed: " + error.message)
  });

It is difficult to tell without being able to see the shape of your data, but I would imagine the best way to go about doing what you are trying is:

  • Query database with the checkoutID
  • Use .remove on returned Ref

Otherwise, this looks like a typo:

for (const res of result){
console.log("result key " + result.payload.key);
// Should be 'res.payload.key' ??
}

Hope some of this helps

Thank you, Sky020 This post must be at least 20 characters so am adding this filler.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.