1

I'm deleting data from my firebase db with fetch but I can't figure out how to point to an exact ID.

const deleteHandler = async (id) => {
console.log(id);
await fetch(
  `https://react-task-tracker-8e519-default-rtdb.firebaseio.com/tasks.json/${id}`,
  {
    method: "DELETE",
  }
);

I tried it this way, but it gives me a CORS error. I'm also displaying data from this db, that works fine.

UPDATE: I also want to say that when i console.log the id it gives me the correct one.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Monard
  • 425
  • 1
  • 5
  • 9

3 Answers3

2

(Tl;dr: Try adding '.json' to the end of the endpoint.)

I would recommend reading this page to get a general understanding of what a CORS error is and why it might be happening.

In your case, I would recommend using the Firebase SDK that is best suited to your application. You could start here and follow the setup instructions for whichever is most applicable to your use case (perhaps the node client sdk)?

If you must avoid using the sdks for some reason then I would refer to some other Stackoverflow questions such as this one, which suggests that all Firebase REST endpoints need to end with '.json'.

tomking
  • 313
  • 1
  • 11
0

You just need to add .json at the end of your request and remove .json from tasks.json. like this:-

await fetch(
  `https://react-task-tracker-8e519-default-rtdb.firebaseio.com/tasks/${id}.json`,
Tyler2P
  • 2,324
  • 26
  • 22
  • 31
ankur
  • 1
0
const deleteHandler = async (id) => {
console.log(id);
await fetch(
  `https://react-task-tracker-8e519-default-rtdb.firebaseio.com/tasks/${id}.json`,
  {
    method: "DELETE",
  }
);

Just replace .json text with ${id}.json.

Have a nice day

F. Müller
  • 3,969
  • 8
  • 38
  • 49