MichaelSolati / geofirestore-js

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

Property 'query' does not exist on type 'typeof GeoFirestore' #136

Closed eibagit closed 5 years ago

eibagit commented 5 years ago

In general, what I'm basically going for is a listener that returns any document that enters an area, and then hopefully I can figure out how to further use those values in an Android app.

I have just recently started learning Geofirestore and I'm loving it so far, but I've been stuck on this problem for two days now, and my (very) limited programming skills don't seem to bring me any further, although I'm relatively sure it will be a quick fix. I have copied some sample code from this repository and from some online examples, and this is what I have:

import * as functions from 'firebase-functions';
import * as firebase from 'firebase/app';
import 'firebase/firestore';

// If you're using ES6+/imports/ESM syntax for imports you can do this:
import { GeoCollectionReference, GeoFirestore, GeoQuery, GeoQuerySnapshot } from 'geofirestore';

// If you're using CommonJS/require syntax for imports you can do this:
//const { GeoCollectionReference, GeoFirestore, GeoQuery, GeoQuerySnapshot } = require('geofirestore');

// Initialize the Firebase SDK
firebase.initializeApp({
  // ...
});

// Create a Firestore reference
const firestore = firebase.firestore();

// Create a GeoFirestore reference
const geofirestore: GeoFirestore = new GeoFirestore(firestore);

// Create a GeoCollection reference
const geocollection: GeoCollectionReference = geofirestore.collection('Users');

const geoQuery = GeoFirestore.query({
  center: new firebase.firestore.GeoPoint(50.9235691,10.7218614),
  radius: 10.5,
  query: (ref) => ref.where('d.details.name', '==', 'Geofirestore')
});

exports.Listen = functions
  .region('europe-west1')
  .https.onRequest(async (req, res) => {
  // Then listen for results as they come in
  const onKeyEnteredRegistration = geoQuery.on('key_entered', (key, document, distance) => {
    console.log(key + ' entered query at ' + document.coordinates.latitude + ',' + document.coordinates.longitude + ' (' + distance + ' km from center)');
    res.status(200).send('Yep');
    return 0;
  })
  .catch(error => error);
});

It is the "GeoFirestore.query({..." that gives the "Property 'query' does not exist on type 'typeof GeoFirestore'" error. I have tried mixing this line up in all sorts of ways, but admittedly it's been a lot of guesswork, and so any help on the matter would be greatly appreciated.

MichaelSolati commented 5 years ago

The sample you've presented is more inline with version 2.x.x rather than 3.x.x. Effectively, we've moved away from a geofire syntax and it looks more like firestore. So it should look like this...

import * as functions from 'firebase-functions';
import * as firebase from 'firebase/app';
import 'firebase/firestore';

// If you're using ES6+/imports/ESM syntax for imports you can do this:
import { GeoCollectionReference, GeoFirestore, GeoQuery, GeoQuerySnapshot } from 'geofirestore';

// If you're using CommonJS/require syntax for imports you can do this:
//const { GeoCollectionReference, GeoFirestore, GeoQuery, GeoQuerySnapshot } = require('geofirestore');

// Initialize the Firebase SDK
firebase.initializeApp({
  // ...
});

// Create a Firestore reference
const firestore = firebase.firestore();

// Create a GeoFirestore reference
const geofirestore: GeoFirestore = new GeoFirestore(firestore);

// Create a GeoCollection reference
const geocollection: GeoCollectionReference = geofirestore.collection('Users');

const geoQuery = geocollection.near({
  center: new firebase.firestore.GeoPoint(50.9235691,10.7218614),
  radius: 10.5
}).where('details.name', '==', 'Geofirestore');

exports.Listen = functions
  .region('europe-west1')
  .https.onRequest(async (req, res) => {
  // Then listen for results as they come in
  geoQuery.get().then((snapshot) => {
    console.log(snapshot.docs);
    res.status(200).send('Yep');
    return 0;
  })
  .catch(error => error);
});

If you need more examples I'd take a look at spittin-hot-geofirestore or the tests for this project.

andresfmm commented 2 years ago

this work for me

const geocollection = await GeoFirestore.collection('employees_actives').where("status", "==", "active")

const geoQuery = geocollection.near({ center: new db.GeoPoint(latitude, longitude), radius: 5, });

// Get query (as Promise) const resultcollection = await geoQuery.get();

console.log( resultcollection )