问题描述
我目前正在使用 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 似乎有同样的问题,唯一的答案是使用放置",但同样会删除所有内容其他我在 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 相当于 push"操作使用客户端 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 代替.这相当于使用 "set" 操作使用客户端 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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!