0

I have a Collection "Users". Each user has some member variables. I am trying to read the data stored in the Co-Email member variable through a document reference.

public String getCoEmail() {
        coUserReference = db.collection("users").document(email);

        coUserReference.get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
            @Override
            public void onSuccess(DocumentSnapshot documentSnapshot) {
                if (documentSnapshot.exists()) {
                    String coEmail = documentSnapshot.getString("coEmail");

                }
                else {
                    Toast.makeText(ViewExpenses.this, "Document does not exist", Toast.LENGTH_LONG).show();
                }
            }
        });

    return coEmail;

    }

It never gets into the OnSuccess part of the code. So I am unable to get the coEmail. Any help would be greatly appreciated.

enter image description here

I have also used this code but I am having the same issue. It never gets past the addOnCompleteListener line. (Doesn't get into onComplete)

public String getCoEmail() {
 
coUserReference = db.collection("users").document(email);

coUserReference.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
    @Override
    public void onComplete(@NonNull Task<DocumentSnapshot> task) {
        if (task.isSuccessful()) {
            DocumentSnapshot document = task.getResult();
            if (document.exists()) {

            String coEmail = document.getString("coEmail");
            Log.d("TAG", "CoEmail:  " + coEmail);

        }
        else {
            Toast.makeText(ViewExpenses.this, "Document does not exist", Toast.LENGTH_LONG).show();
        }
    }}
});



    return coEmail;

    }
Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
Elby
  • 11
  • 3
  • 2
    If the query fails, you would never know because you don't have any error handling. I suggest reading over the [documentation](https://firebase.google.com/docs/firestore/query-data/get-data#get_multiple_documents_from_a_collection) again to learn how to do that. It shows using an onCompleteListener which will receive any possible errors. If an error happens, you should log the exception to find out what went wrong. – Doug Stevenson Aug 20 '20 at 17:09
  • I tried this as well. Based on the documentation mentioned above. I hit the same problem. – Elby Aug 20 '20 at 18:50
  • Please try to use onComplete Listener instead of on Success Listener. – Gouse Mohiddin Aug 20 '20 at 18:58
  • I got it! Thank you both- using the OnComplete Listener with various error handling worked, – Elby Aug 20 '20 at 19:15
  • could you share the error this is throwing as well as your datastructure in firestore? – Soni Sol Aug 20 '20 at 19:16
  • You cannot be that `coEmail` object as a result of a method. Firebase API is asynchronous. So please check the duplicate to see how can you solve this using a custom callback. – Alex Mamo Aug 28 '20 at 07:08

0 Answers0