在Firestore中创建文档时如何使用自定义文档ID

时间:2020-05-20 05:45:44

标签: firebase google-cloud-firestore

在Firebase Firestore的“身份验证和数据库”部分中创建用户时遇到问题。

用户注册时,将生成一个新的ID(身份验证中的UID),并生成另一个ID(在用户集合下的文档ID)。当用户创建帐户时,我试图使身份验证方面的两个ID和文档ID相同。

这是我的代码:

firebaseAuth.createUserWithEmailAndPassword(userEmail, userPassword)
                    .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                        @Override

                        public void onComplete(@NonNull Task<AuthResult> task) {
                            // Sign up success -> user data should store inside db
                            if (task.isSuccessful()) {

                                firebaseAuth.getCurrentUser().sendEmailVerification().addOnCompleteListener(new OnCompleteListener<Void>() {
                                    @Override

                                    public void onComplete(@NonNull Task<Void> task) {
                                        if (task.isSuccessful()) {     //registered successfully

                                            user.put("email", email.getText().toString());
                                            user.put("password", password.getText().toString());

                                            //To write a user to the database
                                            mDatabase.collection("users").add(user)
                                                    // successfully add to db
                                                    .addOnSuccessListener(new OnSuccessListener<DocumentReference>() {
                                                        @Override
                                                        public void onSuccess(DocumentReference documentReference) {
                                                            Toast.makeText(getBaseContext(), "SUCCESS: Your account was created successfully! Please check your email for verification. User "
                                                                    + email.getText().toString() + " has been added to the database.", Toast.LENGTH_LONG).show();

                                                            Intent loginActivity = new Intent(getApplicationContext(), LoginActivity.class);
                                                            startActivity(loginActivity);
                                                        }
                                                    })

                                                    .addOnFailureListener(new OnFailureListener() {
                                                        @Override
                                                        public void onFailure(@NonNull Exception e) {
                                                            Toast.makeText(getBaseContext(), "Error adding to DB", Toast.LENGTH_LONG).show();
                                                        }
                                                    });

                                        }
                                        // task was unsuccessful
                                        else {
                                            String fail = "FAIL: User failed to register: " + task.getException();
                                            Toast.makeText(getBaseContext(), fail, Toast.LENGTH_LONG).show();
                                            Log.e("", fail);
                                        }
                                    }
                                });
                            }
                            else{
                                String fail = "FAIL: User failed to register: " + task.getException();
                                Toast.makeText(getBaseContext(), fail, Toast.LENGTH_LONG).show();
                                Log.e("", fail);
                            }
                        }
                    });`

我尝试使用

设置数据
FirebaseFirestore mDatabase = FirebaseFirestore.getInstance();
DocumentReference usersRef = mDatabase.collection("users").document(currentUser);
String id = firebaseAuth.getUid();
usersRef.document(id).set(user);

替换

中的代码部分
Map<String, Object> user = new HashMap<>();
user.put("name", name.getText().toString());
user.put("email", email.getText().toString());
user.put("password", password.getText().toString());

但是我在代码的.addOnSuccessListener(new OnSuccessListener<DocumentReference>()部分出现了错误

enter image description here

我是Firebase的新手,所以我们将不胜感激:)

0 个答案:

没有答案