在没有唯一键的情况下向 firebase 发布请求 [英] Post request to firebase without unique key

查看:22
本文介绍了在没有唯一键的情况下向 firebase 发布请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将新数据发布到我的 firebase API,但每次这样做时,都会生成一个新密钥,例如 -L545gZW7E6Ed6iqXRok,其中包含我的对象.我想在没有这个新密钥的情况下将我的对象直接保存到 API.这个问题的答案如何使用 set() 方法来做到这一点,但我想使用 Postman 来实现这一点.我使用 Postman 直接发布到 firebase.url: https://my-firebase-project.firebaseio.com/galaxies.json 使用 POST 方法.

I want to post new data to my firebase API, but everytime I do so, a new key, like -L545gZW7E6Ed6iqXRok is generated with my object inside it. I would like to save my object directly to the API without this new key. This SO question answers how to do it using the set() method, but I would like to achieve this using Postman. I am posting directly to firebase using Postman. url: https://my-firebase-project.firebaseio.com/galaxies.json with method POST.

//current saving like this in firebase
"0000001" : {
  "active": false,
  "name": "tp-milky-way",
  "time": 60
},
"-L545gZW7E6Ed6iqXRok": {
	"0000011": {
      "active": false,
      "name": "tp-andromeda",
      "time": 60
    }
}

//I want it without the key
"0000001" : {
  "active": false,
  "name": "tp-milky-way",
  "time": 60
},
"0000011" : {
  "active": false,
  "name": "tp-andromeda",
  "time": 60
}

我发现我可以将 PUT 与整个 json 对象一起使用,该对象最初是通过添加或删除放置"到 firebase 的,并且 firebase 将新放置请求与那里已有的内容进行比较并相应地更新.我不知道这种行为是我所理解的,或者是否没有更好的方法来添加没有自动生成的密钥的数据.

I found out I can use PUT with the entire json object that was originally 'put' to firebase with the additions or deletions, and firebase compares the new put request with what's already on there and updates accordingly. I don't know the behaviour is as I understand it or if there isn't a better way to add data without auto-generated keys.

推荐答案

当您使用 POST 动词,Firebase 生成一个新位置.这符合 REST-ful 习惯用法:POST 用于在服务器定义的新位置创建新对象.

When you use the POST verb, Firebase generates a new location. This is in line with REST-ful idioms: POST is used to create a new object in a server-defined new location.

如果您想写入现有位置或您控制的新位置,请使用 PUT 动词.在这种情况下,数据将准确写入您在 URL 中指定的位置,并且会覆盖该位置的所有现有数据.

If you want to write to an existing location, or a new location you control, use the PUT verb. In this case the data will be written to exactly the location you specify in the URL, and it will overwrite any existing data at that location.

如果您想更新现有位置的部分数据,但不修改其他数据,请使用 PATCH 动词.

If you want to update part of the data at an existing location, but leave other pieces of the data unmodified, use the PATCH verb.

如果您的 HTTP 客户端不支持指定动词,您可以选择 将动词作为 HTTP-Method-Override 标头传递.

If your HTTP client doesn't support specifying a verb, you can optionally pass the verb as HTTP-Method-Override header.

这篇关于在没有唯一键的情况下向 firebase 发布请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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