0

I'm just wanna create an account with some data. Unfortunately, I do not know where the error is, and during registration, the account is created but the data is not saved in the Realtime Database.

mAuth.createUserWithEmailAndPassword(email, password).addOnCompleteListener(task -> {
    if (task.isSuccessful()) {
        FirebaseUser user = mAuth.getCurrentUser();
        reference = FirebaseDatabase.getInstance().getReference("Users").child(user.getUid());

        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("username", username);
        hashMap.put("email", email);
        hashMap.put("id", user.getUid());
        hashMap.put("imageURL", "default");
        hashMap.put("status", "offline");

        reference.setValue(hashMap).addOnCompleteListener(task1 -> {
            if (task1.isSuccessful()) {
                Toast.makeText(RegisterActivity.this, "Registered Successfully", Toast.LENGTH_SHORT).show();
                startActivity(new Intent(RegisterActivity.this, StartActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK));
            }else{
                Toast.makeText(RegisterActivity.this, "Registered Failed", Toast.LENGTH_SHORT).show();
            }
        });
    }
});
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
Magnsi
  • 1
  • 2

1 Answers1

2

When a Task fails, it has an exception in it that contains details about what failed. Log the exception to find the root cause of the problem:

reference.setValue(hashMap).addOnCompleteListener(task1 -> {
    if (task1.isSuccessful()) {
        Toast.makeText(RegisterActivity.this, "Registered Successfully", Toast.LENGTH_SHORT).show();
        startActivity(new Intent(RegisterActivity.this, StartActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK));
    }else{
        Toast.makeText(RegisterActivity.this, "Registered Failed", Toast.LENGTH_SHORT).show();
        Log.e("FIREBASE", "Registering user in database failed", task.getException()); // 
    }
});

Once you get the exception, try to address what it says, or search for the error message to see if somebody else had the same problem before.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • You might want to check your app's logcat output for relevant messages in that case. For example: https://stackoverflow.com/questions/68806876/firebase-realtime-database-connection-killed-different-region. If none of that is the case, [enabling debug logging](https://firebase.google.com/docs/reference/android/com/google/firebase/database/FirebaseDatabase.html#setLogLevel(com.google.firebase.database.Logger.Level)) might allow you to see more on the cause of the problem. – Frank van Puffelen Nov 20 '21 at 20:41
  • thanks, it help me a lot – Magnsi Nov 20 '21 at 23:04