Delete file on realtime Database on Firebase with React.js

Viewed 77

This is the database link

const firebase = "https://booktakeaway-dd150-default-rtdb.europe-west1.firebasedatabase.app"

And this the function that i use to delete the file, but i doesn't work. I can't find anything online.

return async dispatch => {
    try {
      const response = await firebase.get(`booksData.json?auth=${token}`);
      const firebaseData= response.data;
      for (let key in dataFirebase) {
        if (id === firebaseData[key].bookId) {
          await firebase.ref(`/booksData/${key}`).remove();
          console.log("Done");
        }
      }
      await dispatch(getSavedBooksIDs());
    } catch (error) {
      console.log(error);
    }
  };

Structure of the databse

1 Answers

You can try to set it to null, worked for me:

return async dispatch => {
    try {
      const response = await firebase.get(`booksData.json?auth=${token}`);
      const firebaseData= response.data;
      for (let key in firebaseData) {
        if (id === firebaseData[key].bookId) {
          await firebase.ref(`booksData/${key}`).set(null);
          console.log("Done");
        }
      }
      await dispatch(getSavedBooksIDs());
    } catch (error) {
      console.log(error);
    }
  }; 
Related