Reputation:
I'm trying to display user data in the app using user id for retrieval. The problem is that this code pops out the second option which is that the document referring to the user was not found.
String userUid = FirebaseAuth.getInstance().getCurrentUser().getUid();
DocumentReference documentReference = firebaseFirestore.collection("Users").document(userUid);
documentReference.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()) {
DocumentSnapshot document = task.getResult();
if (document.exists()) {
Log.d(TAG, "Données : " + document.getData());
} else {
Log.d(TAG, "Pas de données trouvées");
}
} else {
Log.d(TAG, "Une erreur est survenue ", task.getException());
}
}
});
documentReference.addSnapshotListener(getActivity(), new EventListener<DocumentSnapshot>() {
@Override
public void onEvent(@Nullable DocumentSnapshot documentSnapshot, @Nullable FirebaseFirestoreException e) {
pseudo.setText(documentSnapshot.getString("Pseudo"));
name.setText(documentSnapshot.getString("Name"));
surname.setText(documentSnapshot.getString("Surname"));
mail.setText(documentSnapshot.getString("Mail"));
age.setText(documentSnapshot.getString("Age"));
}
});
If instead of "userUid" in DocumentReference I use a Uid write directly (for exemple : ZxjVbnC5uodcQC5729sn) it works but with the variable it does not.
EDIT 1
Here is a screen of the database :
If I display it in the logcat I realize that the id retrieve is not the one corresponding to the user.
D/Données de l'utilisateur: Pas de données trouvées njo4FfZNQLfHktDRzwfYMjmLO6G2
That of the logged-in user is: ZxjVbnC5uodcQC5729sn
And the one found by getUid() is : njo4FfZNQLfHktDRzwfYMjmLO6G2
I don't understand why this is not showing the correct Uid...
Upvotes: 1
Views: 72
Reputation: 138824
Since the UID of the logged-in user is different than the one in the database, you get the expected behavior. You cannot get something from a node that actually doesn't exist. Most likely, you used that user earlier, and somehow you deleted the account only from the Firebase Console and you left the corresponding data in the database. The simplest solution would be to write the data again using the current UID. Right after that, your code will work.
Upvotes: 1