如何获取文件ID Firestore?

时间:2019-09-23 08:47:21

标签: java android google-cloud-firestore

我试图获取用于更新数据的文档ID,但是它创建了一个新的文档ID。如何获取文件ID Firestore?

My firestore

代码:

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

1 个答案:

答案 0 :(得分:1)

它没有更新,因为:

下面的代码行会在每次调用getID并存储在String中时生成新的id。

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

因此,我建议您将从getID商店获得的ID存储在每个用户文档下。例如,当您创建/生成文档时,只需在userid字段下添加id的String值。