简体   繁体   English

Android Firestore 使用 Uid 显示用户数据

[英]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:这是数据库的屏幕:

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.

相关问题 用户 UID 与 Firestore Android 中的文档 ID 不同 - The User UID is not the same with document id in Firestore Android 如何使用 Java 在 Android Studio 中输入用户的 uid 来更新或删除 Firestore 中的数据? - How to renew or delete the data in Firestore by entering the uid of user in Android Studio with Java? 使用 Firebase 数据库显示来自用户 uid 的数据 - Display data from user uid with Firebase Database Android FireStore 显示数据 - Android FireStore display data 使用uid为当前用户显示RealTime数据库中的某些数据 - Display certain data from RealTime Database for the current user using uid 如何在android studio中显示firestore数据库中navigation header中的用户数据 - How to display the user data in navigation header from firestore database in android studio 如何使用 Android 在 RecyclerView 中显示来自 Firestore 的数据? - How to display data from Firestore in a RecyclerView with Android? 如何从 android 中的 FireStore 检索用户数据 - how to retrieve user data from FireStore in android 我需要显示存储在 Cloud Firestore android studio 中的数据 - I need to display data stored in cloud firestore android studio JAVA:在来自 Firestore 的 android 中的图像 slider 中显示数据 - JAVA : Display data in image slider in android from firestore
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM