1

This is my Firebase database structure

STUDENT
 ur14cs002
    id:ur14cs002
    pass:abc
 ur14cs001
    id:ur13cs001
    pass:def

I have to check with my user's id like when user enter in edit text that needs to be check if that particular user exists in the database or not. Below code this is what i did bt this code is not working at all.

  EditText id = (EditText) findViewById(R.id.userEmail);

    EditText password = (EditText) findViewById(R.id.userPassword);
    database = FirebaseDatabase.getInstance();

    String child = id.getText().toString();
    String pass = password.getText().toString();
    myRef = database.getReference("STUDENT").child(child);
    myRef.orderByChild("id").equalTo(child).addValueEventListener(new ValueEventListener(){
        @Override
        public void onDataChange(DataSnapshot dataSnapshot){
            if(dataSnapshot.exists() ){
                Toast.makeText(getApplicationContext(),"PRESENT",Toast.LENGTH_LONG).show();
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }


    });}}
0

1 Answer 1

1

Your reference is wrong:

 myRef = database.getReference("STUDENT").child(child);

In the above the dataSnapshot will be the child which is this id:ur14cs002 for example. So ofcourse it will never exists, since the dataSnapshot will be the child (which is the id provided).

You need to do this:-

  myRef = database.getReference().child("STUDENT");

This way the reference is at node STUDENT then orderByChild("id").equalTo(child) will give you the right result. Here the dataSnapshot will be the (STUDENT) node.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.