0

I have the following firestore query below. I am trying to perform multiple where query on the Book collection. I want to filter by book name and book age range. However i am getting the following error

"uncaught error in onsnapshot firebaseError: cursor position is outside the range of the original query" can someone please advise.

           const collectionRef = firebase.firestore().collection('Books')

           collectionRef.where('d.details.BookType',"==",BookType)
           collectionRef = collectionRef.where('d.details.bookage',"<=",age)
           collectionRef = collectionRef.orderBy('d.details.bookage')


             const geoFirestore = new GeoFirestore(collectionRef)

              const geoQuery = geoFirestore.query({
              center: new firebase.firestore.GeoPoint(lat, long),
              radius: val,

              });

            geoQuery.on("key_entered",function(key, coords, distance) {   

storeCoordinate(key,coords.coordinates._lat,coords.coordinates._long,newdata)
});
Graig Peru
  • 107
  • 1
  • 3
  • 12
  • Have you tried to use a chain call like `const geoFirestore = new GeoFirestore(collectionRef.where('d.details.BookType',"==",BookType).where('d.details.bookage',"<=",age).orderBy('d.details.bookage'))` instead of this `const geoFirestore = new GeoFirestore(collectionRef)`? – Alex Mamo Nov 01 '18 at 06:05
  • I just tried however it did not work am not sure what the issue is. – Graig Peru Nov 01 '18 at 07:27
  • Did you have the same behaviour the above solution? – Alex Mamo Nov 01 '18 at 07:33
  • yea the error message is the same as the initial error. Are you able to replicate ? – Graig Peru Nov 01 '18 at 07:37
  • I believe the orderBy syntax is causing the issue however i do need the order by because i am doing a filter on book age. – Graig Peru Nov 01 '18 at 08:07
  • If you remove `collectionRef = collectionRef.orderBy('d.details.bookage')` does it work? – Alex Mamo Nov 01 '18 at 08:15
  • No, if i remove the orderBy clause it begins to complain about the <= operator. – Graig Peru Nov 01 '18 at 08:24
  • Giving what message? – Alex Mamo Nov 01 '18 at 08:33
  • message is : "firebase Invalid query you have a where filter with an inequality on field d.details.age and must also use d.details.age as your first Query.ordeby() but your Query.orderBy() is on filed g instead" how can we remove the geofirestore orderby on the g index an if we do so how will this affect geofirestore query. – Graig Peru Nov 01 '18 at 08:40
  • Have you also tried to use `const geoFirestore = new GeoFirestore(collectionRef.orderBy('d.details.bookage').where('d.details.bookage',"<=",age).where('d.details.BookType',"==",BookType))`? See the new order? Does it work now? – Alex Mamo Nov 01 '18 at 08:53
  • the latest suggested solution did not work, it gave the initial error of"uncaught error in onsnapshot firebaseError: cursor position is outside the range of the original query" – Graig Peru Nov 01 '18 at 10:32
  • can we remove the geofirestore OrderBy on the g field ? by default geofirestore is ordering by the g filed as the first oderby and we need d.details.age to be the first orderBy. – Graig Peru Nov 01 '18 at 10:34
  • Give it a try. Does it work? – Alex Mamo Nov 01 '18 at 10:41
  • It didn’t work @ Alex – Graig Peru Nov 01 '18 at 13:25
  • Do you have another error? – Alex Mamo Nov 01 '18 at 13:27
  • Your solution is the last thing I tried and it did not work i. e const geoFirestore = new GeoFirestore(collectionRef.orderBy('d.details.bookage').where('d.details.bookage',"<=",age).where('d.details.BookType',"==",BookType)). I would like to go ahead and remove the geofirestore orderBy on the g field however not sure how I can achieve that. Any ideas? – Graig Peru Nov 01 '18 at 13:34
  • I suggest you create queries by adding function call step by step and see where it is going wrong. – Alex Mamo Nov 01 '18 at 13:35
  • I am not sure if I understand what you mean can you share the proposed ? – Graig Peru Nov 01 '18 at 13:40
  • a duplicate was found at https://stackoverflow.com/questions/51484558/using-external-orderby-without-using-used-query-inequality-in-firebase-firestore?fbclid=IwAR2gjlRiOeRQe-7BoqQ5dV8gQ5nTPuWwCd9_10Ba3roSC87HUml6mcXIa6M however still no real solution. – Graig Peru Nov 01 '18 at 22:26
  • I'd check out Frank van Puffelin's response to a similar question. https://stackoverflow.com/questions/50390366/error-cursor-position-is-outside-the-range-of-the-original-query#comment87800847_50390366 – MichaelSolati Nov 05 '18 at 22:38
  • So micheal in a buy shell this isn’t possible at this time ? – Graig Peru Nov 05 '18 at 22:47
  • It isn't really possible, no. Additional filters will have to take place on the client. – MichaelSolati Nov 06 '18 at 14:01

1 Answers1

1

Internally geoFirestore gets its results by this._query.orderBy('g').startAt(query[0]).endAt(query[1])

Laying it out sequentially, expanding your collectionRef, something like this is happening:

const collectionRef = firebase.firestore().collection('Books')
collectionRef.where('d.details.BookType',"==",BookType)
collectionRef = collectionRef.where('d.details.bookage',"<=",age)
collectionRef = collectionRef.orderBy('d.details.bookage')
collectionRef.orderBy('g').startAt(query[0]).endAt(query[1])

The problem happens because .startAt is referring to your first orderBy which is d.details.bookage, so it is doing start at the cursor where d.details.bookage is query[0].

Seeing that query[0] is a geohash, it translates to something like start at the cursor where d.details.bookage is w2838p5j0smt, hence the error.


Solution

There are two ways to workaround this limitation.

  1. Wait for an update on Geofirestore which I think @MichaelSolati is already working on.
  2. Sort the results after getting results from Geofirestore's onKey
Community
  • 1
  • 1
Joshua Chan
  • 1,797
  • 8
  • 16
  • Unfortunately no, I will not be able to update the library to address this. But I do have other things I look to address (I just need to write tests for the library before a launch of v3) – MichaelSolati Nov 07 '18 at 01:31
  • 1
    @MichaelSolati I saw that you were working on allowing users to get a snapshot of the results without needing to aggregate them in `onKeyEntered`, that would help step 2 a lot I think. Looking forward to v3. – Joshua Chan Nov 07 '18 at 01:42