MichaelSolati / geofirestore-js

Location-based querying and filtering using Firebase Firestore.
https://geofirestore.com
MIT License
504 stars 58 forks source link

Using .where in a query #66

Closed 24dev closed 5 years ago

24dev commented 5 years ago

Currently have a functioning 'get restaurants' function, which returns all restaurants in a radius using geofirestore. Awesome. But when I add a .where() clause to the query, it stops working. The .limit() on it's own works fine - has anyone tried this yet, and have they managed to get it working?

Code here:

export const getRestaurants = params => {
  const { location } = params;
  function getQuery() {
    return new Promise((resolve, reject) => {
      const results = [];

      const geoQuery = restaurantsRef.query({
        center: new firebase.firestore.GeoPoint(location[1], location[0]),
        radius: 1.5,
        query: ref => ref.where('approved', '==', true).limit(25),
      });
      geoQuery.on('key_entered', ($key, result) => {
        result.$key = $key;
        results.push(result);
      });
      geoQuery.on('ready', () => {
        resolve(results);
      });
    });
  }

  return getQuery().then(res => res);
};

Error here:

screen shot 2019-01-03 at 17 26 20

Also worth pointing out the link https://geofirestore.com/examples/ is broken, so no examples!

MichaelSolati commented 5 years ago

I have not actually noticed any issue like that, are you getting an error? There is now an error listener you can use (which could be helpful, but you'll have to update to version 2.4.0).

geoQuery.on('error', (error) => {
    console.log(error.message);
});

As a side note, the example thing doesn't work and isn't necessarily intended to(?) It's something that needs to be done at some point. Anyway the only examples available are here => Viewers App Spittin Hot Geofirestore

Keep in mind they're made with version 3.0.0 in mind.

24dev commented 5 years ago

Ah yes I do get an error - Uncaught Error in onSnapshot: Error: The query requires an index. You can create it here:

Unfortunately, the link provided does not work. I assume the index is required because geofirestore is filtering the store by location, and then we're having the chained .where on top. Have you had to create an index for geofirestore before? I've made plenty of indexes but i'm not sure exactly what geofirestore is looking for when it does its filtering, so wouldn't know what to put in the index.. @MichaelSolati

Especially since the data gets stored by geo in an object d: {...},, can you even do a deep index like that? d.approved, in my example?

MichaelSolati commented 5 years ago

The link should work, as it's produced by Firebase (not geofirestore).

But this library actually uses an indexed collection for testing (meaning yes, you can do a deep index).

So here we have the index you see below, and that works fine:

{
  "indexes": [
    {
      "collectionId": "tests",
      "fields": [
        { "fieldPath": "d.count", "mode": "ASCENDING" },
        { "fieldPath": "g", "mode": "ASCENDING" }
      ]
    }
  ]
}

You could do something like that.