0

I am currently using axios to post/get from my Firebase database in my React application. I had no issue until I attempted to make my first POSTs to the database. It is successfully POSTing, but I am getting a random string for an ID instead of it directly posting into the place in my database I want.

random id incorrectly inserted Instead of being preregs -> group -> 2020 -> userID: value as I want it, I'm getting prereg -> random key -> group -> 2020 -> userID: value.

Currently, my POST code reads as:

axios.post('/preregs.json', {
        [this.props.group]: {
            [this.props.currentYear]: {
                [this.props.userID]: document.getElementsByTagName("input")["email"].value
            }
        }
    })

I have heard that if you don't use POST to put data into a Firebase database, it won't create that random ID. However, patch won't work as it just deletes all the data I had within 2020. I looked at other questions posted such as Setting custom key when pushing new data to firebase database, but it is using Firebase to do the work instead of Axios. Also Firebase POST custom ID who seems to have the same issue, with the only answering being to use 'put, but again that erases everything else I have in 2020. Is there something I am missing to stop Firebase from putting this random key when I don't want it to?

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
Hitokage
  • 201
  • 2
  • 12

1 Answers1

4

According to the REST API documentation for Realtime Database, a POST is the equivalent of a "push" operation when using the client SDKs. This push operation always involves adding data under a random ID. There is no avoiding that for a push.

If you know the name of the node where you want to add data, you should use a PUT instead. This is the equivalent of using "set" operation with the client SDKs.

Since you are running in a javascript client, consider also just using the client SDK, as it will be easier and more efficient to work with than the REST API.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • Doug, thank you for answering. However, when I attempted to use 'PUT', as I said in my original post, it would only replace all of the data I already had within 2020 instead of adding to it. Are the only options 'random ID' or 'replace all data'? There is no way to add without the random ID on a RTD? – Hitokage Jan 20 '20 at 19:55
  • Use PATCH if you want to update data without overwriting. This is all part of the documentation. – Doug Stevenson Jan 20 '20 at 19:58
  • I used axios.patch, however, it proceeded to do the same thing as put - replace the data. Hence my needing to post this question to SO. – Hitokage Jan 20 '20 at 20:02
  • You are setting data at the top level of the tree, starting with /preregs, which will overwrite everything under that node. Instead specify the full path of the node to udpate using the path of the request instead of specifying the entire object of data to /preregs.json. For example: /preregs/group/year/userID.json – Doug Stevenson Jan 20 '20 at 20:06
  • Ah! That makes sense! I didn't realize it was considered replacing them since the other items I had in 'group' did not get replaced. Thank you for timely, well written answers and your help. – Hitokage Jan 20 '20 at 20:10