当写入数据库时​​,注册后数据库仍然为空。如何将数据正确存储在数据库中?

时间:2019-02-18 15:44:01

标签: android firebase-realtime-database

我正在写Firebase实时数据库。如何正确写入数据库?

没有发现错误。但是我将将实时数据库添加到应用程序的依赖性从com.google.firebase:firebase-database:16.0.1更改为com.google.firebase:firebase-database:16.0.6

这是定义的变量

private DatabaseReference mDatabase;

这是我的注册活动代码部分

private void register_user (final String displayName, String email, String password){

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

            if (task.isSuccessful()) {


                FirebaseUser current_user = FirebaseAuth.getInstance().getCurrentUser();
                String uid = current_user.getUid();

                mDatabase = FirebaseDatabase.getInstance().getReference().child("Users").child(uid);

                HashMap<String,String> userMap = new HashMap<>();
                userMap.put("name",displayName);
                userMap.put("status","Hey there I am using ChatApp");
                userMap.put("image","default");
                userMap.put("thumb_image","default");

                mDatabase.setValue(userMap);
            } else {

                mRegProgress.hide();
                Toast.makeText(RegisterActivity.this, "Couldn't Sign in.Please Check and Try Again", Toast.LENGTH_LONG).show();

            }
        }
    });
}

数据条目应存储在数据库中

1 个答案:

答案 0 :(得分:0)

Firebase Realtime Database的默认安全规则拒绝读取和写入。因此,您需要在Firebase控制台中检查和更改安全规则。

将安全规则更改为

    {
      "rules": {
        "Users": {
           "$uid": {
             ".read": "$uid === auth.uid",
             ".write": "$uid === auth.uid"
           }
        }
      }
    }

有关更多安全选项,请查看firebase docs