0

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.

Jonathan
  • 63
  • 6
  • 1
    Log the error, e, in the failure listener and see what error firestore is throwing. From there we can now know what is the problem from the error's code – Obum Jul 05 '22 at 20:29
  • Have you tried in your code `documentRef.get().addOnSuccessListener(new OnSuccessListener() {` as in [this example?](https://stackoverflow.com/q/48499310/17544309) – Osvaldo Jul 05 '22 at 22:53
  • Have you tried to use `Log.d(TAG, "Download Failure: " + e.exception!!.message!!);`? Do you get something printed out? Please respond with @AlexMamo – Alex Mamo Jul 06 '22 at 18:28

0 Answers0