我正试图在我的实时数据库中创建一个名为"users"的文件夹,该文件夹的唯一标识符是Firebase创建的UID。
这个代码几个月前就起作用了。但现在不行了。坦率地说,我做了很多改变,我不知道这是不是我删除的东西。。。但我找不到哪里出了问题。创建了用户,但在我的实时数据库中的"用户"内部没有任何内容。
auth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(SignUpActivity.this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
auth.signInWithEmailAndPassword(inputEmail.getText().toString(),inputPassword.getText().toString());
Toast.makeText(SignUpActivity.this, "createUserWithEmail:onComplete:" + task.isSuccessful(), Toast.LENGTH_SHORT).show();
progressBar.setVisibility(View.GONE);
// If sign in fails, display a message to the user. If sign in succeeds
// the auth state listener will be notified and logic to handle the
// signed in user can be handled in the listener.
if (!task.isSuccessful()) {
Toast.makeText(SignUpActivity.this, "Authentication failed." + task.getException(),
Toast.LENGTH_SHORT).show();
} else {
auth.signInWithEmailAndPassword(inputEmail.getText().toString(),inputPassword.getText().toString());
FirebaseUser firebaseUser = auth.getCurrentUser();
String userid = firebaseUser.getUid();
final User newuser = new User(signupInputUsername.getText().toString().trim(), inputEmail.getText().toString().trim(), inputPassword.getText().toString().trim(),
gender.getText().toString().trim(), age.getText().toString().trim());
mDatabase.child("users").child(userid).setValue(newuser);
startActivity(new Intent(SignUpActivity.this, UserActivity.class));
finish();
}
}
});
@IgnoreExtraProperties
public static class User {
public String username, inputEmail, inputPassword, gender, age;
public User() {
// Default constructor required for calls to DataSnapshot.getValue(User.class)
}
public User(String username, String inputEmail, String inputPassword, String gender, String age) {
this.username = username;
this.inputEmail = inputEmail;
this.inputPassword = inputPassword;
this.gender = gender;
this.age = age;
}
}
从您的代码中查看以下代码
mDatabase.child("users").child(userid).setValue(newuser);
startActivity(new Intent(SignUpActivity.this, UserActivity.class));
finish();
方法.setValue
实际上为您提供了一个任务,它不是一个void函数,因此您应该为它附加一个监听器,并查看任务是否已完成。
如果它以前工作过,那么它仍然是代码中的一个问题。
此外,如果数据库规则正常,请查找它们。