【发布时间】:2023-03-11 02:40:01
【问题描述】:
我是使用实时数据库的新手,并决定构建一个还包含用户注册和登录的项目。 为此,我使用了 firebase autentication 系统,但因为它只使用电子邮件和密码,所以我想在实时数据库中保留更多信息,这样我就可以使用为每个用户提供的 uid 进行访问。
注册过程运行良好,我可以在 firebase autentication 中看到新用户,但是 出于某种我不明白的原因,无论我尝试什么,我尝试保存的额外信息都不会上传到数据库中。
这是我写的代码
public void tryRegister(View view){
authService.createUserWithEmailAndPassword
(this.email.getText().toString(),this.password.getText().toString())
.addOnCompleteListener(this, task -> {
if (task.isSuccessful()){
String uid = authService.getCurrentUser().getUid();
Map<String, String> userDetails = new HashMap<String, String>();
userDetails.put("username", this.username.getText().toString());
DatabaseReference usersInfo = database.getReference("usersInfo");
usersInfo.child(uid).setValue(userDetails);
setResult(RESULT_OK);
finish();
}
else{
this.failedReg.setText("error in the registration process");
}
});
}
应用级依赖:
dependencies {
implementation platform('com.google.firebase:firebase-bom:28.3.1')
implementation 'com.google.firebase:firebase-analytics'
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'com.google.android.material:material:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'com.google.firebase:firebase-auth:21.0.1'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
implementation platform('com.google.firebase:firebase-bom:28.3.1')
implementation 'com.google.firebase:firebase-database'}
【问题讨论】:
-
你的 onComplete 是否被触发了?
-
如果在
tryRegister方法调用的开头database.getReference("test").setValue(true);会写入数据库吗?如果不是,则问题与身份验证无关,纯粹与数据库访问有关。 -
嗨!我想是这样,但想知道我是否错过了什么。无论如何,我将读写规则设置为true,但我仍然无法将所需的信息上传到实时数据库。你能给我指示我应该检查的其他事情吗?谢谢!
标签: java android firebase firebase-realtime-database