My app tries to retrieve the value of a field in a document in Firebase.
The structure of the Firebase
database is very simple: The root collection
is called "Users". It contains a single document
, called "vfS6yOAJsjR49eWcXOeIvZCulJl2" (i.e. the userID
automatically generated by Firebase). This document contains a single field, which is called "stockList". This field contains a JSON String
(all verified through Firebase Console
).
The code I'm using to retrieve the value of the field "stockList" is:
private FirebaseFirestore db;
private DocumentReference documentRef;
private FirebaseAuth auth;
private FirebaseUser currentUser;
private String json;
auth = FirebaseAuth.getInstance();
currentUser = auth.getCurrentUser();
Log.d(TAG, "UserID: "+ currentUser.getUid() );
if(currentUser != null){
documentRef = db.collection("Users").document(currentUser.getUid());
documentRef.get().addOnSuccessListener(documentSnapshot -> {
Log.d(TAG, "onSuccess: OK");
json = documentSnapshot.getString("stockList");
}
.addOnFailureListener(e -> {
Log.d(TAG, "Download Failure" );
}
}
The Logcat always says "Download Failure" (json
remains indeed empty), though the user is properly authenticated by Firebase (currentUser.getUid() == "vfS6yOAJsjR49eWcXOeIvZCulJl2"
).
My Firebase security rules
are:
service cloud.firestore {
match /Users/{userID} {
allow read, write : if request.auth != null && request.auth.uid == userId;
}
}
Your advice is much appreciated.
PS: I'm test-running the app through the Emulator in Android Studio. So internet connectivity shouldn't be the cause.