Home > other >  Delete node firebase in react
Delete node firebase in react

Time:09-22

I'm building a blog in react and trying to add some functionality to delete the entire blog from memory where it is stored in realtime database on firebase but can't seem to get the remove function working.

I've read the API but it doesn't really elaborate on the .remove method and I'm struggling to get it functioning. At the moment I'm getting to the point of accessing the right file passed by blogID but removing it doesn't seem to work!

Hoping someone might be able to point out the correct way to remove the data as the other posts I've also looked at haven't really helped.

At the moment this is what I have:

export async function handleDelete(blogID) {
  const dbRef = await ref(getDatabase());
  const getBlog = await get(child(dbRef, `blogs/${blogID}`));
  getBlog.remove;
}

CodePudding user response:

The get() returns a DataSnapshot that does not have a remove() property. Instead you need to use remove() function with a DatabaseReference when using the Modular SDK. Try the following:

import { remove } from "firebase/database";

export async function handleDelete(blogID) {
  const blogRef = await ref(getDatabase(), `blogs/${blogID}`);
  const getBlog = await get(blogRef);
  await remove(blogRef);
}
  • Related