用户uid与firestore android中的文档id不同

zpf6vheq  于 2021-07-09  发布在  Java
关注(0)|答案(1)|浏览(403)

我创建了一个函数,用于将新用户添加到firestore数据库:

public void addNewUser(String email, String username, String profile_photo){
        FirebaseFirestore db = FirebaseFirestore.getInstance(); // this is instantiated here, just to show you 
        User user = new User(userID, (long) 1, email, StringManipulation.condenseUsername(username), username, profile_photo);

        db.collection("Users").add(user)
                .addOnSuccessListener(new OnSuccessListener<DocumentReference>() {
                    @Override
                    public void onSuccess(DocumentReference documentReference) {
                        Log.d(TAG, "DocumentSnapshot added with ID: " + documentReference.getId());
                    }
                })
                .addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception e) {
                        Log.w(TAG, "Error adding document", e);
                    }
                });
    }

以及一个从数据库获取数据的函数,等待所有字段出现在应用程序中。

public UserSettings RetrieveUserSettings(){
        Log.d(TAG, "getUserAccountSettings: retrieving user account settings from firebase");

        User settings = new User();
        DocumentReference userRef;
        FirebaseAuth firebaseAuth=FirebaseAuth.getInstance();

                try {
                    userRef = db.collection("Users")
                                .document(firebaseAuth.getCurrentUser().getUid());

                    userRef.get()
                            .addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
                                @Override
                                public void onSuccess(DocumentSnapshot documentSnapshot) {
                                    if(documentSnapshot.exists())
                                    {
                                        settings.setDisplay_name(documentSnapshot.getString("display_name"));
                                        settings.setUsername(documentSnapshot.getString("username"));
                                        settings.setProfile_photo(documentSnapshot.getString("profile_photo"));
                                        settings.setEmail(documentSnapshot.getString("email"));
                                        settings.setPhone_number(documentSnapshot.getLong("phone_number"));
                                        settings.setUser_id(documentSnapshot.getString("user_id"));
                                    }
                                    else{
                                        Log.d(TAG, "doc not fount in getUserSettinghs ");
                                    }
                                }
                            })
                            .addOnFailureListener(new OnFailureListener() {
                                @Override
                                public void onFailure(@NonNull Exception e) {
                                    Log.d(TAG, "onFailure: failed to fetch data");
                                }
                            });

                } catch (NullPointerException e) {
                    Log.d(TAG, "getUserAccountSettings: NULLPointerException: " + e.getMessage());
                }
                Log.e(TAG, "getUserAccountSettings: retrieved user_account_settings information: " + settings.toString());

        return new UserSettings(settings);
    }

当我进行身份验证时,这是生成的用户uid:e5bbl0d9lcsky5a0ydujjumvuut2,但是当我调用addnewuser函数时,我的firestore数据库如下所示:

,所以很明显,身份验证时生成的用户uid与文档id不同,即:xwwnrbkliejxsxokqune。您知道为什么会发生这种情况吗?

kq4fsx7k

kq4fsx7k1#

正如@henrytwist在他的评论中提到的,当使用以下代码行时:

db.collection("Users").add(user).addOnSuccessListener(/* ... /*);

您正在使用随机id将新的“user”对象添加到“users”集合中。上面的代码行执行与以下相同的操作:

db.collection("Users").document().set(user).addOnSuccessListener(/* ... /*);

当调用collectionreference的add()方法或collectionreference的document()方法而不传递任何参数时,firestore使用的内置生成器每次调用其中一个方法时都会生成一个随机id。
从下面的代码行可以看出:

userRef = db.collection("Users").document(firebaseAuth.getCurrentUser().getUid());

您已经实现了firebase身份验证,这很好。也就是说,当您要向数据库添加新的“user”对象时,请使用来自身份验证过程的uid,如下所示:

String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
db.collection("Users").document(uid).set(user).addOnSuccessListener(/* ... /*);

这样,文档的id将与数据库中“user\ id”属性的值匹配。

相关问题