Pfil Mintz
Pfil Mintz

Reputation: 161

Android Firebase realtime database replacing data instead of adding new data

I am trying to insert data into Firebase realtime database. The logic of my app is to create a new field containing username and password under users column in the database when the user is signing up.

The app requires an email, username, and password when signing up. I am using Firebase Auth to do this. When I run the app, I can sign up and insert the values in the database successfully, but the values are being replaced in the database whenever a new user signs up instead of creating a new user. Please help, if I am doing something wrong. This is my code.

firebaseAuth.createUserWithEmailAndPassword(email, password)
                .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        UserInformation userInformation = new UserInformation(username,password);
                        //checking if success
                        if(task.isSuccessful()){

                            databaseReference.child("users").setValue(userInformation);
                            Toast.makeText(MainActivity.this,"Successfully registered",Toast.LENGTH_LONG).show();
                            finish();
                            startActivity(new Intent(getApplicationContext(), ProfileActivity.class));
                        }else{
                            //display some message here
                            Toast.makeText(MainActivity.this,"Registration Error",Toast.LENGTH_LONG).show();
                        }
                        progressDialog.dismiss();
                    }
                });

Upvotes: 2

Views: 5658

Answers (2)

Hamza Fareed
Hamza Fareed

Reputation: 1

Use this query. It will definitely start working for you.

FirebaseDatabase.getInstance().getReference("users").child(user.getUid()).setValue(user);

Upvotes: 0

Yoganand
Yoganand

Reputation: 96

databaseReference.child("users").setValue(userInformation)

This will replace the existing data. If you want to add the value as a new child, you will need to use push() , this will generate a unique id and then insert the new data. Try the code below.

databaseReference.child("users").push().setValue(userInformation)

Upvotes: 6

Related Questions