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)
});
22
  • 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)? Commented Nov 1, 2018 at 6:05
  • I just tried however it did not work am not sure what the issue is. Commented Nov 1, 2018 at 7:27
  • Did you have the same behaviour the above solution? Commented Nov 1, 2018 at 7:33
  • yea the error message is the same as the initial error. Are you able to replicate ? Commented Nov 1, 2018 at 7: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. Commented Nov 1, 2018 at 8:07

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

2 Comments

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 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.