在没有唯一密钥的情况下将请求发布到Firebase [英] Post request to firebase without unique key

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

问题描述

我想将新数据发布到我的firebase API中,但是每次这样做,都会在其中包含对象的情况下生成一个新密钥,例如-L545gZW7E6Ed6iqXRok.我想将我的对象直接保存到API中,而无需此新密钥. 这个SO问题回答如何使用set()方法来做到这一点,但我想使用Postman来实现.我使用Postman直接发布到Firebase. 网址: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
}

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

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风格的习惯用法: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.

如果要写入现有位置或您控制的新位置,请使用

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.

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

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客户端不支持指定动词,则可以选择

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天全站免登陆