简体   繁体   中英

how to delete firebase realtime database child node with key?

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"
    }
  }
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM