Ritesh Sachan
Ritesh Sachan

Reputation: 19

dataSnapShot.getChildren() returning null

I'm trying to fetch the user details of a particular user using a query:

    DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
    Query query = reference
            .child("user_account_settings"))
            .orderByChild("user_id")
            .equalTo(getItem(position).getUser_id());
    query.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            Log.d(TAG, "onDataChange: the fetched user id is " + getItem(position).getUser_id());
            for(DataSnapshot singleSnapshot : dataSnapshot.getChildren()){
                //do stuff
            }

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

Firebase database screenshot

The user id that is fetched is correct according to the database and it clearly has children, but for some reason dataSnapshot.getChildren() is empty thus not allowing me to iterate through the foreach loop.

Upvotes: 1

Views: 199

Answers (1)

Alex Mamo
Alex Mamo

Reputation: 138824

The problem in your code lies in the fact that the user ID property is called in the database userid, while in your query you are using user_id, which is incorrect. The name of the field in your query must match the name field in the database. To solve this, simply change:

.orderByChild("user_id")

To:

.orderByChild("userid")

Upvotes: 2

Related Questions