1

I am trying to add some users in my real time firebase data base, I want to check that by email address which is an User attribute, the problem is that it is adding duplicates. I have an button To add in my database

DAOUser daoUser = new DAOUser();//database accessObject


        registerAccountButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (emailField.getText().toString().equals("") || passwordField.getText().toString().equals("") || repeatPasswordField.getText().toString().equals("")) {
                    Toast.makeText(getApplicationContext(), "Please complete all fields!", Toast.LENGTH_SHORT).show();
                } else if (!Objects.equals(passwordField.getText().toString(), repeatPasswordField.getText().toString())) {
                    Toast.makeText(getApplicationContext(), "Passwords does not match!", Toast.LENGTH_SHORT).show();
                } else {
                    addUser(daoUser, emailField.getText().toString(), passwordField.getText().toString());
                }
            }
        });

The method to add in database:

    public void addUser(DAOUser daoUser, String inputMail, String inputPassword) {
        daoUser.getDatabaseReference().child("User").orderByChild("email").equalTo(inputMail).addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {
                if (snapshot.exists()) {
                    Toast.makeText(getApplicationContext(), "User already exists!", Toast.LENGTH_SHORT).show();
                } else {
                    User newUser = new User(inputMail, inputPassword, false);
                    try {
                        daoUser.add(newUser).addOnSuccessListener(suc -> {
                            Toast.makeText(getApplicationContext(), "Successfully registered user!", Toast.LENGTH_SHORT).show();
                        }).addOnFailureListener(er -> {
                            Toast.makeText(getApplicationContext(), "Unable to register user!", Toast.LENGTH_SHORT).show();
                        });
                    } catch (UserIsNullException e) {
                        e.printStackTrace();
                        System.out.println("User is null");
                    }
                }
            }

            @Override
            public void onCancelled(@NonNull DatabaseError error) {
                Toast.makeText(getApplicationContext(), "User DataBase error !", Toast.LENGTH_SHORT).show();
            }
        });
    }

The database looks like this: enter image description here

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • 1
    Your data structure still looks the same as in your [previous question](https://stackoverflow.com/questions/70226676/realtime-firebase-set-rules-to-check-prevent-duplicates). As I said there (and further explained in the questions I linked there), there's no way to guarantee uniqueness of `email` values with this structure. – Frank van Puffelen Dec 04 '21 at 22:20
  • Have you tried to use Frank van Puffelen's solution in the [previous question](https://stackoverflow.com/questions/70226676/realtime-firebase-set-rules-to-check-prevent-duplicates)? – Alex Mamo Dec 07 '21 at 08:04

0 Answers0