firebase REST API POST(推送)子项目的唯一标识以及顶级项目 [英] firebase REST API POST (push) unique id for child item as well as top item

查看:131
本文介绍了firebase REST API POST(推送)子项目的唯一标识以及顶级项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序用户可以创建链,然后其他用户可以添加链接链。当用户创建一个新的链,他们创建一个链接(不应该有一个没有链接的链)。

所以我想做类似这个:

curl -X POST -d'{1:这条链的第一个链接}' https://myapp.firebaseio.com/chains.json '



一个独特的时间戳ID为新的链,但问题是,我也想要一个唯一的时间戳ID为第一个链接,而不是1,以便它很好地适合其他链接将推



我收集了多个请求,可以使用javascript sdk等全部放在一起,但是如何使用REST API执行此操作?



编辑:我没有提到我想要在一个请求中完成所有操作,而不是创建一个空链作为一个请求,然后将第一个链接推到另一个请求上

解决方案 push()生成的 / code>方法。要了解更多信息,请阅读有关推送ID

生成两个推送ID的片段(使用JavaScript语法):

  var ref = new Firebase('https://myapp.firebaseio.com/chains'); 
var newChain = ref.push();
console.log(newChain.key());
var newLink = newChain.push();
console.log(newLink.key());
newLink.set('这条链的第一个链接');

最后一行是此代码段中唯一一次调用Firebase服务器的时间。所有其他操作都是客户端。



由于您没有使用Firebase SDK,因此无法使用上面的JavaScript代码段。但是,您可以在上面的代码片段中做与客户端做的事情相同的事情:生成一个推送ID客户端,然后发送一个命令到REST API。

Firebase发布了用于在JavaScript中生成推送ID的代码 Github:

In my app users can create "chains" and then other users can add "links" to chains. When a user creates a new chain they create a link with it (there should be no such thing as a chain with no links).

So i want to do something like this:

curl -X POST -d '{ "1": "First link for this chain" }' 'https://myapp.firebaseio.com/chains.json'

Which will create a unique timestamp id for the new chain, but the problem is that i also want a unique timestamp id for the the first link, instead of the "1", so that it fits in nicely with the rest of the links that will be pushed on later.

I gather that multiple requests can be all put together using the javascript sdk etc, but how do i do this with the REST API?

EDIT: i didn't mention i want to do it all in one request, rather than create an empty chain as one request and then push the first link on with another request

解决方案

When you call POST on the REST API, the Firebase server generates a unique ID. But for platforms where there is a Firebase SDK (e.g. JavaScript, iOS, Android), the unique ID that is generated by the Firebase client.

The unique IDs that Firebase generates are also often called "push IDs", because they are generated by the JavaScript push() method. To learn more about them, read this article about push IDs.

A snippet that generates two push IDs (in JavaScript syntax):

var ref = new Firebase('https://myapp.firebaseio.com/chains');
var newChain = ref.push();
console.log(newChain.key());
var newLink = newChain.push();
console.log(newLink.key());
newLink.set('First link for this chain');

That last line is the only time in this snippet that any call is made to the Firebase servers. All other operations are client-side.

Since you are not using a Firebase SDK, you cannot use the JavaScript snippet above. But you can do the same thing as the client does in the above snippet: generate one of the push IDs client-side and then send a single command to the REST API.

The code for generating push IDs in JavaScript was published by Firebase on Github:

这篇关于firebase REST API POST(推送)子项目的唯一标识以及顶级项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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