0

My Firestore Collection/Document

`

db.collection("users").document("user").set(map).addOnCompleteListener

`

https://gist.github.com/Lsortudo/52dcce39ffc7c7db358779c87611936b ( Line 70 ) That's my code above... Soo, what i want to do is basically generate User 1, User 2, User 3, but when using 'user' it ends up replacing the previous one (that's something obvius, but idk how to put increment), as would be done to put something in the documentPath to auto-increment or something else that results in creating User1,2, 3

I tried placing an ${counter} and on isSuccessful i would do Counter++, but it keeps going back to 1 (u can see on gist)

Leozinho
  • 3
  • 1

1 Answers1

0

The private var contador = 1 would be 1 every time you restart the application and so the counter restarts every time. It is not recommended to use sequential IDs in Firetore, checkout Limitations of using sequential IDs in Cloud Firestore for more information.

User's Firebase Auth UID is generally used as the Firestore document ID so it is easier to write security rules as well so try this instead:

db.collection("users").document(Firebase.auth.currentUser.uid).set(map)...
Dharmaraj
  • 47,845
  • 8
  • 52
  • 84
  • Thanks, it worked. I have something to ask, if i wanted to get data from my Firestore i'm trying to use this code but it's not working and returns nulls on my TextView's i'm using that code below to get my current user ------------- val user = Firebase.auth.currentUser and i'm using that code below to get info about my user ----------- db.collection("users").document(Firebase.auth.currentUser!!.uid).get() Also using Toast i could see that UID was different from auth/firestore, soo how can i get data back for that user on firestore? – Leozinho Oct 28 '22 at 11:59
  • @Leozinho I don't see any code that is related to fetching data. Also it might be best to post a new question with the relevant code including the Firestore query, the text views that you are referring to and any screenshot of the errors so others can take a look as well. – Dharmaraj Oct 28 '22 at 17:22