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

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


        
3条回答
  •  无人及你
    2021-01-01 03:44

    You may try this.

                    final String userName = unameEditText.getText().toString();
                    databaseReference.child("users").orderByChild("username").equalTo(userName).addListenerForSingleValueEvent(
                            new ValueEventListener() {
    
                                @Override
                                public void onDataChange(DataSnapshot dataSnapshot) {
    
                                    Log.i(Constants.TAG, "dataSnapshot value = " + dataSnapshot.getValue());
    
                                    if (dataSnapshot.exists()) {
    
                                        // User Exists
                                        // Do your stuff here if user already exists
                                        Toast.makeText(getApplicationContext(), "Username already exists. Please try other username.", Toast.LENGTH_SHORT).show();
    
                                    } else {
    
                                        // User Not Yet Exists
                                        // Do your stuff here if user not yet exists
                                    }
                                }
                                @Override
                                public void onCancelled (DatabaseError databaseError){
    
                                }
                            }
    
                    );
    

提交回复
热议问题