Home > Net >  How to check if username already exists in firebase Realtime Database
How to check if username already exists in firebase Realtime Database

Time:02-01

I am trying to check were the users already exists with the same username here is example

enter image description here

Here is my code,

 DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Users");
 Query checkUser = reference.orderByChild("userName").equalTo(userEnteredUserName);

but without the uid it works perfect, but whenever there is uid unable to check the username, Is there is anyway to check username

CodePudding user response:

The query is expecting userName to be a direct field in username node and because userId is not same for everyone you cannot specify a neste path. If you remove the username node and restructure the DB as shown below, the same query should work perfectly:

Users
  | // remove [username] node from here
  |-userId  // userId from Firebase Auth
    |
    |-userName
    |-otherFields

If you want to retain existing structure then you can just check if a node with given username exists:

DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Users").child("userName");
  •  Tags:  
  • Related