Home > OS >  how to delete firebase realtime database child node with key?
how to delete firebase realtime database child node with key?

Time:06-09

When I try to run the code below I get an error and I'm not sure why.

confirmDelete(e) {
  const db = getDatabase();
  db.ref("/ships/"   e.target.id).remove();
},

If I log the e.target.id I get the exact same as the shipKey in the code below.

Here's an example of what that database looks like:

{
  "ships": {
    "-N43Q4E2ruMpyfaIHGDK": {
      "date": "2022-08-06T18:00",
      "name": "ORANGE OCEAN",
      "shipKey": "-N43Q4E2ruMpyfaIHGDK"
    }
  }
}

CodePudding user response:

It seems you are using Firebase Modular SDK (V9.0.0 ) where both ref() and remove() are top-level functions. Try refactoring the code as shown below:

// import the functions
import { getDatabase, ref, remove } from "firebase/database";

confirmDelete(e) {
  const db = getDatabase();
 
  // create DatabaseReference
  const dbRef = ref(db, "/ships/"   e.target.id);

  remove(dbRef).then(() => console.log("Deleted"))
},

Checkout the documentation to learn more about the new syntax.

  • Related