简体   繁体   中英

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?

我的火炉

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM