Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete object from list via firebase rest api

Tags:

firebase

Currently I can't seem to find a way to delete an object from firebase list via REST api. For example I am trying to remove this from the list: https://mrdapper.firebaseio.com/v0/users/41/favs.json?orderBy=%22id%22&equalTo=107657061

Posting a DELETE request doesn't work with query parameter.

like image 759
puppymaster Avatar asked Mar 14 '23 00:03

puppymaster


1 Answers

You can't delete with a query, (although that would be awesome). But you can use the results to send a DELETE request.

Do a GET:

GET https://mrdapper.firebaseio.com/v0/users/41/favs.json?orderBy=%22id%22&equalTo=107657061

This will return the object and you can send a DELETE request for each item it returns.

DELETE https://mrdapper.firebaseio.com/v0/users/41/favs/<returned-id>.json

Now, you may not like sending one delete request per object. But, with your data structure this is necessary.

If you'd like to easily query and delete items, you could try this data structure:

/users/$user_id
/userFavs/$user_id/$fav_id

Store the favs in it's own location under the root. This will allow you to retrieve user data without always getting the favs.

For userFavs if you key off userid and the favid you can easily query and delete.

{
  "userFavs": {
    "41": {
      "107657061": {
        "note_count": 43633
      }
    }
  }
}

Now you can easily get all of the the user's favorites by specifying the user's id. If you need to delete by an id, that is also a key. So you can now DELETE without querying.

DELETE https://mrdapper.firebaseio.com/v0/userFavs/41/107657061.json
like image 193
David East Avatar answered Mar 28 '23 17:03

David East