Hitokage
Hitokage

Reputation: 210

How do you stop the random ID when using Axios to post to Firebase?

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?

Upvotes: 0

Views: 1797

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317760

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.

Upvotes: 4

Related Questions