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.
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;
}