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

查看:95
本文介绍了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 autho生成的密钥作为文档密钥),并运行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天全站免登陆