我正在尝试通过使用Cloud Firestore将数据添加到我的Firebase数据库中,但是它没有添加数据,也没有显示任何错误

时间:2018-09-03 08:54:55

标签: android firebase google-cloud-firestore

这是注册新用户的方法,注册后,我使用用户ID将用户数据添加到数据库中:

public void registerNewEmail(final String email, String password, final TextInputLayout mEmailInputTextLayout, final TextInputLayout mPasswordInputLayout, final String firstName, final String lastName){


    mAuth.createUserWithEmailAndPassword(email, password)
            .addOnCompleteListener(new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {

                    if (task.isSuccessful()) {
                        // Sign in success, update UI with the signed-in user's information
                        Log.d(TAG, "register user : starting ");
                        Log.d(TAG, "createUserWithEmail:success");
                        FirebaseUser user = mAuth.getCurrentUser();

                        assert user != null;
                        userID = user.getUid();

                        Log.d(TAG, "UserID: " + userID);
                        Toast.makeText(mContext,"Sign Up was Successful", Toast.LENGTH_SHORT).show();
                        sendVerificationEmail();

                        addNewUser(firstName, lastName, email, userID);


                    } else {
                        // If sign in fails, display a message to the user.
                        Log.w(TAG, "createUserWithEmail:failure", task.getException());
                        if(task.getException() instanceof FirebaseAuthUserCollisionException){
                            mEmailInputTextLayout.setError(mContext.getString(R.string.email_already_used_error));
                        }
                        if(task.getException() instanceof FirebaseAuthWeakPasswordException){
                            mPasswordInputLayout.setError(mContext.getString(R.string.weak_password));
                        }
                        mProgressBar.setVisibility(View.GONE);
                    }


                }
            });
}

这是我使用firestore引用将数据添加到firebase数据库中的方法:

/*
* Add new usser to Database
* */
private void addNewUser(String firstName,String lastName,String email,String userID){
    String name = firstName + " " + lastName;
    User user = new User(name,email);
    Log.d(TAG, "addNewUser: " + "userID: " + userID);

    mRef.collection(mContext.getString(R.string.DB_users)).document(userID).set(user);


    mAuth.signOut();
    ((Activity)mContext).finish();
}

1 个答案:

答案 0 :(得分:0)

您可以将mRef.collection(mContext.getString(R.string.DB_users)).document(userID).set(user);替换为:

db.collection(mContext.getString(R.string.DB_users)).document(userID)
        .set(user)
        .addOnSuccessListener(new OnSuccessListener<Void>() {
            @Override
            public void onSuccess(Void aVoid) {
                Log.d(TAG, "DocumentSnapshot successfully written!");
            }
        })
        .addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                Log.w(TAG, "Error writing document", e);
            }
        });