Home > Back-end >  How to add data to the firebase firestore by generating different document id after hitting submit b
How to add data to the firebase firestore by generating different document id after hitting submit b

Time:09-15

I am trying to push the form data to the firebase-firestore. And I also did it successfully. But, the problem is that whenever I am trying to submit the form data again and again it just updates the last data with the current data.

Actually, my requirement is that whenever the user hit the submit button. It creates a document with a random id and stores the all data into that specific id that is generated.

enter image description hereenter image description here

CodePudding user response:

You are specifying the document ID in .document() so it'll overwrite the same document. If you want a document with a random ID on every click, try using add() instead as shown below:

val collectionRef =  FirebaseFirestore.getInstance().collection("Maintainance")

collectionRef.add(user).addOnCompleteListener(...)

Alternatively, you can leave .document() empty to get a DocumentReference with a random ID:

val userDocument = FirebaseFirestore.getInstance().collection("Maintanance").document() // <-- don't pass an ID
  • Related