0

This code is returning an error. Can anyone please guide me on what is wrong with it?

DatabaseReference dbRef = FirebaseDatabase.getInstance().getReference("Doctor");

dbRef.keepSynced(true);

DatabaseReference dbRef2 = FirebaseDatabase.getInstance().getReference("Patient").child(FirebaseAuth.getInstance().getCurrentUser().getUid());
Query query = dbRef.orderByChild("filter").equalTo(passed,dbRef2.child("location").toString());

FirebaseRecyclerOptions<DoctorHelperClass> options = new FirebaseRecyclerOptions.Builder<DoctorHelperClass>()
                .setQuery(query, DoctorHelperClass.class).build();
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Haneen
  • 51
  • 1
  • 7

1 Answers1

1

The problem is likely here:

Query query = dbRef.orderByChild("filter").equalTo(passed,dbRef2.child("location").toString());

In case there are multiple nodes with the same filter value., the second parameter to equalTo is used to further limit the results returned. It does not indicate the name of the property to filter on, which you may seem to think.

The full path to filter on, needs to be in the call to orderByChild. So if you want to order/filter on the filter/location property of each child node, that'd be:

Query query = dbRef.orderByChild("filter/location").equalTo(passed);

Update: since you indicated wanting to order/filter on two properties.

Firebase Database queries can only order/filter on a single property. In many cases it is possible to combine the values you want to filter on into a single (synthetic) property. In your case for example you could order/filter on "location_specialization": "locationValue_specializationValue".

For an example of this and other approaches, see my answer here: Query based on multiple where clauses in Firebase

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • but I want it to equate to two different values. Apart from the location, I want to compare the specialization as well. How can I do that? – Haneen Jul 08 '21 at 09:31
  • 1
    Firebase Database queries can only order/filter on a single property. In many cases it is possible to combine the values you want to filter on into a single (synthetic) property. In your case for example you could order/filter on `"location_specialization": "locationValue_specializationValue"`. For an example of this and other approaches, see my answer here: https://stackoverflow.com/questions/26700924/query-based-on-multiple-where-clauses-in-firebase – Frank van Puffelen Jul 08 '21 at 12:52