How to search if a username exist in the given firebase database?

前端 未结 3 869
不思量自难忘°
不思量自难忘° 2021-01-01 02:59
{
 users:
  {
    apple:
     {
       username :  apple
       email    :  apple@xy.com
       uid      :  tyutyutyu
     }
    mango:
     {
       username :  man         


        
3条回答
  •  抹茶落季
    2021-01-01 03:47

    EDIT: New answer, old one still below.

    I would get rid of your method "checkFirebaseForUsername" because it will always return 0, no matter what.

    What you need to do is this:

    DatabaseReference ref = FirebaseDatabase.getInstance().getReference();
    ref.child("users").child("username").addListenerForSingleValueEvent(new ValueEventListener() {
      @Override
      public void onDataChange(DataSnapshot dataSnapshot) {
         if(dataSnapshot.exists()){
          // use "username" already exists
          // Let the user know he needs to pick another username.
        } else {
          // User does not exist. NOW call createUserWithEmailAndPassword
          mAuth.createUserWithPassword(...);
          // Your previous code here.
    
        }                               
      }
    
      @Override
      public void onCancelled(DatabaseError databaseError) {
    
      }
    });
    

    Old Answer:

    {
     users:
      {
        apple[X]:
         {
           username :  apple[Y]
           email    :  apple@xy.com
           uid      :  tyutyutyu
         }
        mango:
         {
           username :  mango
           email    :  mango@xy.com
           uid      :  erererer
         }
      }
    }
    

    If for example, the node apple[X] will always have the same name as the child property "username":apple[Y], then it is as simple as this.

    DatabaseReference ref = FirebaseDatabase.getInstance().getReference();
    ref.child("users").child("username").addListenerForSingleValueEvent(new ValueEventListener() {
      @Override
      public void onDataChange(DataSnapshot dataSnapshot) {
         if(dataSnapshot.exists()){
          // use "username" already exists
        } else {
          // "username" does not exist yet.
        }                               
      }
    
      @Override
      public void onCancelled(DatabaseError databaseError) {
    
      }
    });
    

    however, if say, the node apple[X] can have a different value than the property apple[Y], and you want to see if any node exists where the "username" property is the same, then you will need to do a query.

     Query query = FirebaseDatabase.getInstance().getReference().child("users").orderByChild("username").equalTo("usernameToCheckIfExists");
     query.addListenerForSingleValueEvent(new ValueEventListener() {
          @Override
          public void onDataChange(DataSnapshot dataSnapshot) {
            if (dataSnapshot.getChildrenCount() > 0) {
                // 1 or more users exist which have the username property "usernameToCheckIfExists"
             }
           }
    
          @Override
          public void onCancelled(DatabaseError databaseError) {
    
          }
      });
    

提交回复
热议问题