firebase/firestore:创建和存储文档的唯一 id(USERID 的用户) [英] firebase/firestore: create and store unique id for documents (users for USERID)

查看:25
本文介绍了firebase/firestore:创建和存储文档的唯一 id(USERID 的用户)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想根据位置为每个用户生成唯一 ID.例如:如果用户来自 newyouk,则 ID 将为 NY-23234.这应该发生在用户第一次注册我的应用时.

I would like to generate unique ID for each user based on the location. eg: if the user is from newyouk, ID would be NY-23234. This should happen the very first time when the user sign up to my app.

我看到我可以使用自动生成的 Firestore ID - 但是分配为 id 时间太长,并且不能包含任何其他字符.我正在使用 angularfire2 连接到 firestore.

I see that I could use auto generated firestore ID - but it is too long for assigning as id and it can't have any other characters with it. I am using angularfire2 to connect to firestore.

我现在的想法是:每次注册新用户时,将用户详细信息保存在用户集合"中(使用 Firestore 自动生成的密钥作为文档密钥)并运行 firebase 函数以在用户"集合中创建项目.它应该返回来自同一位置的最后一个 singup 用户 ID 并在其中添加1".然后将该号码保存到新创建的用户.

What I am thinking now is : every time a new user is signed up, save the user details in 'users collection' (with firestore authogenerated key as the document key ) and run a firebase function for item creation in 'users' collection. It should return the last singup users ID from the same location and add '1' to it. then save that number to the newly created user.

有什么建议吗?任何其他方式来做到这一点.

any suggestions plz? any other way to do it.

推荐答案

如果你使用 angularfire2 你应该使用 createId() 函数:

if you use angularfire2 you shoud use createId() function:

//Defination:
import { AngularFirestore } from 'angularfire2/firestore';
// in constructor
export class Foo{ constructor(private afs: AngularFirestore)}
//then use it:
const id = this.afs.createId();

注意:我不确定这个 ID 是否唯一
编辑
在 firestore 中,每个文档都有唯一的 ID.你可以使用它.例如,您可以通过像这样创建文档来添加额外的字段:

Note: I am not sure if this id is unique
Edit
In firestore every doc has unique id. You could use it. For example you can add extra field by creating doc like this:

this.afs.collection('collection_name').add(your_obj)
.then(ref => {
ref.set({ extra_idField: ref.id }, { merge: true }).then(() => {
console.log("Your extra id field has been created");
});

这篇关于firebase/firestore:创建和存储文档的唯一 id(USERID 的用户)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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