【问题标题】:can't upload data to realtime firebase无法将数据上传到实时 Firebase
【发布时间】: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


【解决方案1】:

当我执行您的代码时,数据会毫无问题地添加到实时数据库中。

我猜你没有在你的 firebase 设置中授予 write 访问权限。请检查您是否在您的规则部分提供了write 访问权限,如下所示

为了您的参考,我已经添加了工作代码。

我刚刚在向数据库添加数据时包含了addOnCompleteListener()。这样应用程序只有在数据成功添加到数据库时才会调用finish()

这也可能是您的问题。因为向实时数据库添加任何值都是异步发生的。在将数据添加到数据库之前关闭应用程序可能会失败。

public class MainActivity extends AppCompatActivity {

    private FirebaseAuth firebaseAuth;

    @Override
    protected void onCreate ( Bundle savedInstanceState ) {
        super.onCreate ( savedInstanceState );
        setContentView ( R.layout.activity_main );
        firebaseAuth = FirebaseAuth.getInstance ();
        Button signIn = findViewById ( R.id.button );
        signIn.setOnClickListener ( v -> tryRegister () );
    }

    public void tryRegister () {
        firebaseAuth.createUserWithEmailAndPassword ( "test@gmail.com", "password" )
                .addOnCompleteListener ( this, task -> {
                    if (task.isSuccessful ()) {
                        String uid = firebaseAuth.getCurrentUser ().getUid ();
                        Map<String, String> userDetails = new HashMap<> ();
                        userDetails.put ( "username", "test@gmail.com" );
                        DatabaseReference usersInfo = FirebaseDatabase.getInstance ().getReference ( "users" );
                        usersInfo.child ( uid ).setValue ( userDetails ).addOnCompleteListener ( task1 -> {
                            Log.d ( "INFORMATION", "Added" );
                            setResult ( RESULT_OK );
                            finish ();
                        } );
                    } else {
                        Log.d ( "INFORMATION", "Error" );
                    }
                } );
    }
}

希望以上解决方案对您有所帮助!

【讨论】:

  • 感谢您的回答!我已经设置了规则,因此它们将与您编写的内容相匹配,并在 setvalue 函数上添加了 oncomlete 侦听器。仍然,我只有现在注册活动完成后才遇到同样的问题。你能告诉我应该在哪里检查问题吗?也许我还没有在我的项目中设置一些东西?
  • 我希望您只使用上面的代码进行新注册。因为上面的代码将被执行,并且只有在发生新注册时才会完成您的活动。如果您尝试使用相同的凭据进行注册,则 task.isSuccessful() 将为 false,因此不会调用 finish()。在重试之前,请删除在 Firebase 控制台的身份验证部分创建的现有用户,然后尝试注册
  • 我正在删除运行代码后添加的用户,但是 task1.isSuccessful()(我们为 setvalue 函数设置的那个)总是返回 false .. 也许我需要一些东西安装不同?我只是不明白为什么它不起作用
猜你喜欢
  • 1970-01-01
  • 2019-06-07
  • 1970-01-01
  • 1970-01-01
  • 2021-01-31
  • 2019-11-26
  • 1970-01-01
  • 1970-01-01
  • 2021-10-07
相关资源
最近更新 更多