How to get document id Firestore?

I was trying to get document id for updating a data, but instead it create a new document id. How to get document id Firestore?

My firestore

Code :

    final String getID = firebaseFirestore.collection("Users").document(currentUser.getUid()).collection("Documents").document().getId();

    buttonUpdate.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            final String inspectorName = editInspector.getText().toString().trim();
            final String marketLocation = editLocation.getText().toString().trim();

            progressUpdated.show();

            firebaseFirestore.collection("Users").document(currentUser.getUid()).collection("Documents").document(getID).update("inspectorName", inspectorName, "marketLocation", marketLocation).addOnCompleteListener(new OnCompleteListener<Void>() {
                 @Override
                  public void onComplete(@NonNull Task<Void> task) {
                       if (task.isSuccessful()) {
                          Toast.makeText(StartCounting.this, "Document updated", Toast.LENGTH_SHORT).show();

                          progressUpdated.dismiss();
                          dialogUpdate.dismiss();
                        }
                   }
             }).addOnFailureListener(new OnFailureListener() {
                  @Override
                  public void onFailure(@NonNull Exception e) {
                      Toast.makeText(StartCounting.this, "Error : " + e.toString(), Toast.LENGTH_LONG).show();

                      progressUpdated.dismiss();
                }
            });
        }
    });

Upvotes: 0

Views: 169

Answers (1)

Ashish
Ashish

Reputation: 6919

It is not updating because :

The following code of line generates new id when you call the getID each time you call it and store in String.

String getID = firebaseFirestore.collection("Users").document(currentUser.getUid()).collection("Documents").document().getId();

So i suggest you to store the id which you get from getID store under each user document. Something like when you create / generate document just add the String value of id under the userid field.

Upvotes: 1

Related Questions