MichaelSolati / geofirestore-js

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

customKey not working on read #221

Closed tzvc closed 3 years ago

tzvc commented 3 years ago

Hi there, I'm trying to use a custom key for my geoqueries instead of the default g but I'm running into some issues.

When I specify a customKey in the GeoFirestore.collection, then create a query from that and run it, the query is run on g.geohash instead of the specified customKey location.geohash.

Upon reading the source code, it appear to me that the customKey is only used when adding new documents.

    const geocollection = GeoFirestore.collection('posts', 'location');
    const geoQuery = geocollection
      .near({
        center: new admin.firestore.GeoPoint(query.centerLat, query.centerLng),
        radius: query.radius,
      })
      .where('community', '==', req.params.community)
      .limit(10);

    const resSnapshot = await geoQuery.get();

Am I missing something here ?

Cheers!

MichaelSolati commented 3 years ago

Instead of an await, try doing a traditional promise and apply the catch. I'd love to see what if any error appears.

tzvc commented 3 years ago

@MichaelSolati With a traditional then/catch, I dont get any errors, but no results (where they should have been)

    const geocollection = GeoFirestore.collection('posts', 'location');
    const geoQuery = geocollection
      .near({
        center: new admin.firestore.GeoPoint(query.centerLat, query.centerLng),
        radius: query.radius,
      })
      .where('community', '==', req.params.community)
      .limit(10);

    return geoQuery
      .get()
      .then(v => res.status(200).json(v.docs))
      .catch(e => console.error(e));

I believe this issue was documented here too: https://github.com/MichaelSolati/geofirestore-js/issues/199

tzvc commented 3 years ago

Also, by looking at the source of geofirestore-core, it doesn't appear to support custom keys as it uses the g key statically https://github.com/MichaelSolati/geofirestore-core/blob/main/src/api/query-get.ts#L83

MichaelSolati commented 3 years ago

What version are you using and can you show me what a document in Firestore looks like?

MichaelSolati commented 3 years ago

The custom key shouldn't matter on query as its used to encode into the .g field.

tzvc commented 3 years ago

@MichaelSolati I'm using 4.4.2 and my documents looks like this

{
  location: {
    geopoint: GeoPoint,
    geohash: string
  },
  // other fields...
} 
MichaelSolati commented 3 years ago

Ok, so the geopoint that you use in your doc can be any field, but you are right that geofirestore uses the g property.

Are you manually creating that location property?

tzvc commented 3 years ago

@MichaelSolati The location property is created in the flutter frontend via https://pub.dev/packages/geoflutterfire, which allows a field name to be specified (which is porb. why I assume this worked the same way)

MichaelSolati commented 3 years ago

Got it, unfortunately no, this library doesn't support custom property names for queries, only to encode the document.

It's a good feature for down the road but not something I see myself getting to any time soon.

tzvc commented 3 years ago

Fair. If I end up implementing it, I'll make sure to submit a PR ;)

In any case, thank you for your time!