使用Axios发布到Firebase时如何停止随机ID? [英] How do you stop the random ID when using Axios to post to Firebase?

查看:85
本文介绍了使用Axios发布到Firebase时如何停止随机ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用axios在我的React应用程序中从Firebase数据库发布/获取.在尝试将第一个POST写入数据库之前,我没有任何问题.它已成功发布,但我得到的是ID的随机字符串,而不是直接将其发布到我想要的数据库中.

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.

我不再需要preregs-> group-> 2020-> userID:value,而是使用prereg-> random key-> group-> 2020-> userID:value.

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

当前,我的POST代码显示为:

Currently, my POST code reads as:

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

我听说,如果您不使用POST将数据放入Firebase数据库中,它将不会创建该随机ID.但是,补丁程序将无法工作,因为它只会删除我在2020年之内的所有数据.我查看了其他发布的问题,例如在推送时设置自定义密钥新数据添加到Firebase数据库,但它使用Firebase代替Axios来完成工作.另外, Firebase POST自定义ID 似乎也有相同的问题,唯一的答案是使用"put",但同样会擦除所有内容还有2020年的其他功能.是否有需要阻止Firebase在我不希望放置此随机密钥的情况下阻止它的功能?

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?

推荐答案

根据实时数据库的REST API文档 POST 等效于推送"操作使用客户端SDK时.此推送操作始终( )涉及在随机ID下添加数据.不能避免这样做.

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.

如果您知道要在其中添加数据的节点的名称,则应使用 PUT 代替.这等效于使用设置"操作客户端SDK.

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.

由于您在javascript客户端中运行,因此也请考虑仅使用客户端SDK,因为与REST API相比,使用它更容易,更有效.

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.

这篇关于使用Axios发布到Firebase时如何停止随机ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆