firebase/firestore: create and store unique id for documents (users for USERID)

☆樱花仙子☆ 提交于 2020-02-04 01:33:08

问题


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.

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.

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.


回答1:


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();

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");
});



回答2:


I was running a similar question in my code. I was needing to create a random key, so, i found this code (that is used by firestore on client side):

  const chars'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
  let autoId = ''
  for (let i = 0; i < 20; i++) {
    autoId += chars.charAt(Math.floor(Math.random() * chars.length))
  }

Check if your user is from NY

If (myUserIsFromNy) {
let ny = "NY ";
let res = ny.concat(autoId);
}

autoId is the generated id by firestore on .add() method.

Instead, you can create your own function and store it in a variable,

let key = myKeyFunction()

a good idea is to do:

  const chars'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
  let autoId = ''
  for (let i = 0; i < 5; i++) {
    autoId += chars.charAt(Math.floor(Math.random() * chars.length))
  }

Do a function to find where your user is located and get the first two letters from the city. To begin this you can check

https://www.w3schools.com/html/html5_geolocation.asp With the lagitude and longitude you can make a function to find where it is, city, etc... Or you can make it easy by asking in the signUp's form.

then:

let ny = "NY-"
let key = ny.concat(autoId)

than you can create a document with:

db.ref.doc(key).set(Data)

Where Data is your user information

Running a cloud function to do this is a bad soluction, because the server will do an extra write to every new user, you should run just 1 time the write and will be running it twice, which will increase your cost with firestore, firestore forces you to do best practices because if not, you will be charged for this.



来源:https://stackoverflow.com/questions/48494604/firebase-firestore-create-and-store-unique-id-for-documents-users-for-userid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!