我试图获取用于更新数据的文档ID,但是它创建了一个新的文档ID。如何获取文件ID 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();
}
});
}
});
答案 0 :(得分:1)
它没有更新,因为:
下面的代码行会在每次调用getID
并存储在String中时生成新的id。
String getID = firebaseFirestore.collection("Users").document(currentUser.getUid()).collection("Documents").document().getId();
因此,我建议您将从getID
商店获得的ID存储在每个用户文档下。例如,当您创建/生成文档时,只需在userid字段下添加id的String值。