[英]Android Firestore display user data with Uid
I'm trying to display user data in the app using user id for retrieval.我正在尝试使用用户 ID 在应用程序中显示用户数据以进行检索。 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.如果不是 DocumentReference 中的“userUid”,而是直接使用 Uid 写入(例如:ZxjVbnC5uodcQC5729sn),它可以工作,但它不能使用变量。
EDIT 1编辑 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.如果我在 logcat 中显示它,我意识到检索的 id 不是与用户对应的。
D/Données de l'utilisateur: Pas de données trouvées njo4FfZNQLfHktDRzwfYMjmLO6G2
That of the logged-in user is: ZxjVbnC5uodcQC5729sn登录用户为:ZxjVbnC5uodcQC5729sn
And the one found by getUid() is: njo4FfZNQLfHktDRzwfYMjmLO6G2 getUid() 找到的是:njo4FfZNQLfHktDRzwfYMjmLO6G2
I don't understand why this is not showing the correct Uid...我不明白为什么这没有显示正确的 Uid ...
Since the UID of the logged-in user is different than the one in the database, you get the expected behavior.由于登录用户的 UID 与数据库中的 UID 不同,因此您会得到预期的行为。 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.
很可能,您之前使用了该用户,并且不知何故您仅从 Firebase 控制台中删除了该帐户,并将相应的数据留在了数据库中。 The simplest solution would be to write the data again using the current UID.
最简单的解决方案是使用当前 UID 再次写入数据。 Right after that, your code will work.
在那之后,你的代码就可以工作了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.