0

This is my structure:

{
users: {
    "aufgdheyrl": {
        id: "152",
        name: "Joe Jackson",
        level: "A2",
        point: 120.0
    },
    "yudwjwnecj": {
        id: "134",
        name: "Samnuel Jackson",
        level: "B3",
        point: 80.5
    },
    "fuwhdkcjdo": {
        id: "188",
        name: "Jack Jack",
        level: "B2",
        point: 50.0
    }
}
}

this is my code for get the selected user only if its level or point value is greater than another one. But the query doesnt't work.

I use mDatabase.child("users").child(id_client) for getting the reference of the selected user.

DatabaseReference mUserRef = mDatabase.child("users").child(id_client);
Query query1 = mUserRef.orderByChild("level").startAt(level);
Query query2 = mUserRef.orderByChild("point").startAt(point);
query1.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            Log.d(TAG, "datasnaphot " + dataSnapshot.toString());
            User user = dataSnapshot.getValue(User.class);
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            // Getting Post failed, log a message
            Log.w(TAG, "getUser:onCancelled", databaseError.toException());
        }
    });
query2.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            Log.d(TAG, "datasnaphot " + dataSnapshot.toString());
            User user = dataSnapshot.getValue(User.class);
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            // Getting Post failed, log a message
            Log.w(TAG, "getUser:onCancelled", databaseError.toException());
        }
    });

for example I have this values:

String id_client = "aufgdheyrl";
double level = 200.0;
String point = "A1";

both query1 and query2 return object but query1 must have null value and not user object Idem if I have level="B1", the query2 return user object and not null value

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • The `level` and `point` values in your last snippet seem inverted. But aside form that, you're querying at the wrong level. See my answer for more on that. – Frank van Puffelen Sep 08 '17 at 01:51

1 Answers1

0

Firebase queries are executed on a list of nodes, and then filter each child node in that list on the property that you specify.

With mDatabase.child("users").child(id_client) you're already in the node of a specific user:

"aufgdheyrl": {
    id: "152",
    name: "Joe Jackson",
    level: "A2",
    point: 120.0
}

Calling orderByChild() here will try to filter each child node of this JSON. Since the child nodes are simple values (e.g. point: 120.0), the filter will not match anything.

You're more likely to get the results you want if you query the list of users for a level:

Query query = mDatabase.child("users").orderByChild("level").startAt("B3");

This query will return a list with yudwjwnecj.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • ok I understand my error, but there is a solution for getting the user with that id and with user.level > level? – Francesco.D.B. Sep 08 '17 at 07:56
  • Firebase Database queries can only order/filter on a single property. You may be able to combine the values you want to filter on into a single property, eg. `"level_point": "A2_120.0"` and then order/filter on that. For more on this, see my answer here: http://stackoverflow.com/questions/26700924/query-based-on-multiple-where-clauses-in-firebase – Frank van Puffelen Sep 09 '17 at 14:33