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

走远了吗. 提交于 2019-12-25 18:43:13

问题


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:

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

回答1:


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);



回答2:


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



来源:https://stackoverflow.com/questions/49918437/how-to-check-if-sub-child-exists-in-firebase-realtime-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!