Graig Peru
Graig Peru

Reputation: 117

FireStore Multiple where query

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)
});

Upvotes: 0

Views: 2840

Answers (1)

Joshua Chan
Joshua Chan

Reputation: 1847

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

Upvotes: 1

Related Questions