Firebase添加用户问题-- JAVA

y3bcpkx1  于 2022-11-27  发布在  Java
关注(0)|答案(1)|浏览(90)

您好,这是我第一次使用Android Studio和一般数据库,我无法将我的应用连接到数据库。在数据库中,我将.write和.read设置为“true”:

{
"rules": {
".read": "true", //"now < 1671750000000", // 2022-12-23
".write":"true" // "now < 1671750000000", // 2022-12-23
  }
}

但无论何时我试图注册一个用户,它都不起作用。这是我用JAVA编写的代码,如果它有帮助的话:

//DATABASE
//Checking if the user is already registered
mAuth.createUserWithEmailAndPassword(email,password)
        .addOnCompleteListener(new OnCompleteListener<AuthResult>() {
            @Override
            public void onComplete(@NonNull Task<AuthResult> task) {

                //if the user has been registered
                if(task.isSuccessful()){
                    User user = new User(fullname, age, email);

                    //sending the user to the DATABASE
                    FirebaseDatabase.getInstance().getReference("Users")
                            .child(FirebaseAuth.getInstance().getCurrentUser().getUid()) //This will return the ID of the Registered User
                            .setValue(user).addOnCompleteListener(new OnCompleteListener<Void>() {
                                @Override
                                public void onComplete(@NonNull Task<Void> task) {
                                    //If the user has been registered and has been inserted into the DB
                                    if(task.isSuccessful()){
                                        Toast.makeText(registerUser.this, "El usuario se ha registrado.", Toast.LENGTH_LONG).show();
                                        pBprogressbar.setVisibility(View.GONE);
                                    }
                                    //if the user doesn't register
                                    else{
                                        Toast.makeText(registerUser.this, "Ha habido un fallo en el registro", Toast.LENGTH_LONG).show();
                                        pBprogressbar.setVisibility(View.GONE);
                                    }
                                }
                            });
                } else {
                    Toast.makeText(registerUser.this, "Ha habido un fallo en el registro", Toast.LENGTH_LONG).show();
                    pBprogressbar.setVisibility(View.GONE);
                }
            }
        });
    }
}

我已经在它上面运行了调试器,结果如下:

但这是我的User构造函数:

package com.example.kmeco;

//所以我们把用户信息存储在对象中,然后我们把它发送给FIREBASE公共类User { public String fullName,age,email;

//Creating two constructors
//First one is an empty public constructor that doesn't accept or return anything
public User(){

}
//Second constructor
public User(String fullName, String age, String email){
    //initializing values
    this.fullName = fullName;
    this.age = age;
    this.email = email;
}

}
问题就在第一:

if(task isSuccessful())

我不知道为什么它不读,
如果你能帮我

brvekthn

brvekthn1#

如果任务失败,则会包含一个异常错误,其中包含有关失败原因的信息。您应该记录该异常错误,以找出问题的根本原因:

mAuth.createUserWithEmailAndPassword(email,password)
    .addOnCompleteListener(new OnCompleteListener<AuthResult>() {
        @Override
        public void onComplete(@NonNull Task<AuthResult> task) {
            //if the user has been registered
            if(task.isSuccessful()){
                ...
            } else {
                // 👇
                Log.e("AUTH", "createUserWithEmailAndPassword failed", task.getException());
                ...
            }
        }
    });

相关问题