Home > front end >  Firebase Querying with 'order by' and 'where' returning null
Firebase Querying with 'order by' and 'where' returning null

Time:04-03

var x = FirebaseFirestore.instance
    .collection('Workouts')
    .doc(FirebaseAuth.instance.currentUser?.uid)
    .collection('allWorkouts')
    .orderBy("Date Added", descending: true)
    .where("Name", isEqualTo: workout.name)
    .limit(limit)
    .snapshots();

I have attached the code I am using to query data from my Firestore database. I have a field called 'Date Added' with a space. I have also added the respective index in firebase, with 'Date Added' and 'Name', both being ascending. I get an error below

enter image description here

Is there something I am doing wrong with the query, possibly due to the space in between the 'Date' and 'Added'?

Picture of the DB below where you can see that there are entries.

enter image description here

enter image description here

CodePudding user response:

Try moving the Where clause before the OrderBy clause, like this:

var x = FirebaseFirestore.instance
    .collection('Workouts')
    .doc(FirebaseAuth.instance.currentUser?.uid)
    .collection('allWorkouts')
    .where("Name", isEqualTo: workout.name)
    .orderBy("Date Added", descending: true)
    .limit(limit)
    .snapshots();

Also, you mentioned that you created a composite index with Date Added and Name fields both ascending, but you are ordering Date Added as descending, try fixing that index as attached in imageCreate composite index

  • Related