简体   繁体   中英

when write to the database,database is still empty after registering.how to store data in my database correctly?

I'm writing to the Firebase realtime database. How to write to the database correctly?

Where is no errors found. But I changed dependencies of adding the realtime database to the app from com.google.firebase:firebase-database:16.0.1 to com.google.firebase:firebase-database:16.0.6 .

This is the defined variable

private DatabaseReference mDatabase;

this is my register activity code part

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();

            }
        }
    });
}

data entries should store in the database

Default security rules of Firebase Realtime Database rejects read and write. So you need to check and change security rules in firebase console.

Change the security rules into

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

For more security options checkout the firebase docs

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM