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.
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):
Check if your user is from NY
autoId is the generated id by firestore on .add() method.
Instead, you can create your own function and store it in a variable,
a good idea is to do:
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:
than you can create a document with:
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.
if you use angularfire2 you shoud use createId() function:
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: