milancodes
milancodes

Reputation: 173

Check if value exists before deleting from Firebase?

I'm currently working on a REST API, written in Node, user-data is stored in Firebase. If my API successfully handles a delete request, it sends back a 204 response code, but should I check first whether the value the user wants to delete exists? I don't feel right about sending back a 204 code, when in fact, nothing happens, if the value doesn't even exist. This is one of my first REST APIs, so I'm not quite sure how I should properly handle this. Could someone show me the right path when it comes to handling DELETE requests?

Upvotes: 0

Views: 814

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317712

All of the Firebase APIs (both Realtime Database and Firestore - you didn't say which) work like this. The success response from a delete indicates that the data is just confirmed not to exist, not that the data was previously present but is now gone. It does not work like files in a filesystem where programs like rm will fail if the file didn't exist.

If you need to know if the data previously existed, but has just been deleted, the only way to know that for sure is using a transaction to do a read-then-write in on operation. This is the only way to know for sure that the delete operation you performed actually just deleted the data.

If it doesn't matter to you if or how the data was removed, or that it never existed, just accept that the delete operation has confirmed that the data doesn't exist.

Upvotes: 2

Related Questions