4

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"
    }
  }
}
Dharmaraj
  • 47,845
  • 8
  • 52
  • 84

1 Answers1

2

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.

Dharmaraj
  • 47,845
  • 8
  • 52
  • 84
  • 1
    Thanks, @Dharmaraj. I hadn't notice i had to use remove() first. I also had to reference the database on your code that you forgot but it was simple detail stuff. you actually helped a lot. Here's the working code: `confirmDelete(e) { const db = getDatabase(); // create DatabaseReference const dbRef = ref(db,"/ships/" + e.target.id); remove(dbRef); },` – UnrecognizedKey Jun 08 '22 at 23:27