Checking if a particular value exists in the Firebase database

前端 未结 6 1978
孤城傲影
孤城傲影 2020-11-22 07:04

I am making an Android application using Firebase realtime database. When a new user registers on my app, that user\'s data is saved in the Firebase database. <

6条回答
  •  梦如初夏
    2020-11-22 07:26

    Try this:

    DatabaseReference ref=FirebaseDatabase.getInstance().getReference().child("Users");
    ref.orderByChild("username").equalTo(Nick123).addValueEventListener(new ValueEventListener(){
      @Override
      public void onDataChange(DataSnapshot dataSnapshot){
          if(dataSnapshot.exist() {
             //username exist
              }
            }
    

    You have to use orderbychild and equalto to check the value if it is there. If you dont use orderbychild and equalto then it will just check if username child node is there and doesnt care about the value.

    this orderByChild("username").equalTo(Nick123) is like saying:

    WHERE username=Nick123
    

提交回复
热议问题