Firebase:如何为密钥生成唯一的数字ID? [英] Firebase: how to generate a unique numeric ID for key?
问题描述
我需要键的数字ID,例如000000001,000000002,00000003,00000004。
我需要它的原因是因为这些ID将成为在线和离线的永久对象ID 。我希望用户只需输入URL/ objects / 00000001即可浏览该对象页面。
我在这里问,因为我想知道如果不使用 .priority
属性等。我想 set
方法可以做到这一点。如果不可能,就告诉我不,我可以接受这个答案。
建议您阅读Firebase文档。具体来说,请参阅Firebase JavaScript网络指南的保存数据部分。
从指南:
由push()生成的唯一标识
调用push()将返回对新数据路径的引用,您可以使用它获取其ID或设置数据的值到它。下面的代码将产生与上面的例子相同的数据,但是现在我们可以访问生成的唯一的推送标识符
var newPostRef = postsRef.push({
author:gracehop,
title:宣布COBOL,一种新的编程语言
);
//通过访问其键
获取由push()生成的唯一ID var postID = newPostRef.key;
来源: https://www.firebase.com/docs/web/guide/saving-data.html
- 推送生成一个新的数据路径,其服务器时间戳记为
键
。这些键看起来像-JiGh_31GA20JabpZBfa
,所以不是数字。 - 如果你想要一个数字唯一的ID,避免覆盖生成的密钥的对象的参数。
- 键(新数据的路径)保证是唯一的,因此使用数字键覆盖它们没有意义。
- 您可以将数字ID设置为对象的子项。 然后,您可以使用 Firebase查询
a>。
/ b>
在JavaScript中,调用push()然后立即调用set()的模式非常普遍,我们可以让它们通过传递要直接设置的数据push()如下。以下两种写入操作都会导致相同的数据被保存到Firebase中:
//这两个方法相当于
postsRef.push()。set({
author:gracehop,
title:宣布COBOL,一种新的编程语言
});
postsRef.push({
author:gracehop,
title:宣布COBOL,一种新的编程语言
});
来源: https://www.firebase.com/docs/web/guide/saving-data.html
I need numeric IDs for human readability. How do I get it in Firebase?
I want numeric ID for keys, e.g. "000000001", "000000002","00000003","00000004".
The reason I need it is because these IDs will become the permanent object ID both online and offline. I want users to be able to browse that object page by just entering URL "/objects/00000001" without efforts.
I am asking here, because I want to know if this can be done without using .priority
, sub-properties, etc. I guess set
method can do it somehow. If it is not possible, just tell me no, I can accept that answer.
I'd suggest reading through the Firebase documentation. Specifically, see the Saving Data portion of the Firebase JavaScript Web Guide.
From the guide:
Getting the Unique ID Generated by push()
Calling push() will return a reference to the new data path, which you can use to get the value of its ID or set data to it. The following code will result in the same data as the above example, but now we'll have access to the unique push ID that was generated
// Generate a reference to a new location and add some data using push() var newPostRef = postsRef.push({ author: "gracehop", title: "Announcing COBOL, a New Programming Language" }); // Get the unique ID generated by push() by accessing its key var postID = newPostRef.key;
Source: https://www.firebase.com/docs/web/guide/saving-data.html
- A push generates a new data path, with a server timestamp as its
key
. These keys look like-JiGh_31GA20JabpZBfa
, so not numeric. - If you wanted to make a numeric only ID, you would make that a parameter of the object to avoid overwriting the generated key.
- The keys (the paths of the new data) are guaranteed to be unique, so there's no point in overwriting them with a numeric key.
- You can instead set the numeric ID as a child of the object.
- You can then query objects by that ID child using Firebase Queries.
From the guide:
In JavaScript, the pattern of calling push() and then immediately calling set() is so common that we let you combine them by just passing the data to be set directly to push() as follows. Both of the following write operations will result in the same data being saved to Firebase:
// These two methods are equivalent postsRef.push().set({ author: "gracehop", title: "Announcing COBOL, a New Programming Language" }); postsRef.push({ author: "gracehop", title: "Announcing COBOL, a New Programming Language" });
Source: https://www.firebase.com/docs/web/guide/saving-data.html
这篇关于Firebase:如何为密钥生成唯一的数字ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!