Dpka
Dpka

Reputation: 246

How to check if sub child exists in Firebase Realtime Database?

My main node Employees has 3 values: Mob. no., UserPIN and empID. I need to check in one of my activities that if the entered UserPIN exists under thos node or not. I am not able to do this by using:

if(datasnapshot.child(enteredUserPIN).exists(){
}

And also I can't change my node layout as I am checking for the Mob. no. as well in another activity. Plus I can't directly give the path till the Mob. no. Please guide.

Employees node:

Employees table

EDIT

My code:

private void mpinexists(){
    Log.d("abcd","mpinexists() reached");
    checkmPINRef.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for(DataSnapshot ds:dataSnapshot.getChildren()){
                if(ds.child(enteredmPIN).exists()){
                    Toast.makeText(getActivity(),"mPIN already exists",Toast.LENGTH_SHORT).show();
                    Log.d("abcd","mpin already exists");

                }
                else{
                    Log.d("abcd","mpin doesn't exists");
                    Toast.makeText(getActivity(),"mPIN doesn't exists",Toast.LENGTH_SHORT).show();
                }
            }
}

My Log:

04-25 21:53:28.889 10147-10147/com.example.abcd.efg D/abcd: mpinexists() reached
04-25 21:53:31.471 10147-10147/com.example.abcd.efg D/abcd: mpin already exists
04-25 21:53:31.472 10147-10147/com.example.abcd.efg D/abcd: mpin doesn't exists

Upvotes: 0

Views: 1739

Answers (3)

Basheer Adel
Basheer Adel

Reputation: 51

ValueEventListener listener = new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot snapshot) {
            if (snapshot.hasChildren()) {
                Toast.makeText(getApplicationContext(), "hasChildren ", Toast.LENGTH_SHORT).show();
                getDesigns();
            }
            else {
                Toast.makeText(getApplicationContext(), "No Data", Toast.LENGTH_SHORT).show();
                loadingPB.setVisibility(View.GONE);
            }
        }
        @Override
        public void onCancelled(@NonNull DatabaseError error) {

        }
    };
    databaseReference.addListenerForSingleValueEvent(listener);

Upvotes: 0

Alex Mamo
Alex Mamo

Reputation: 138824

In order to solve this, you need to use a Query and exists() method on the DataSnapshot object like this:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
Query query = rootRef
    .child("Employees")
    .child(mobileNumber) //Could be: 765432189
    .child(userPin) //Could be: 123457
    .orderBychild("empid")
    .equalsTo(enteredUserPIN);
ValueEventListener valueEventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        if(dataSnapshot.exists()) {
            //Do something
        } else {
            //Do something else
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {}
};
query.addListenerForSingleValueEvent(valueEventListener);

According to your edit, please use the follwing code:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference employeesRef = rootRef.child("Employees");
ValueEventListener valueEventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for(DataSnapshot ds : dataSnapshot.getChildren()) {
            if(ds.child(enteredUserPIN).exists()) {
                //Do something
            } else {
                //Do something else
            }
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {}
};
employeesRef.addListenerForSingleValueEvent(valueEventListener);

Upvotes: 1

hiten pannu
hiten pannu

Reputation: 186

If you can't change the current table layout. You should create a separate node where you just put all the pins as key value pair e.g Pin as Key and true as value. You can check there if the pin exists or not

Upvotes: 0

Related Questions