MichaelSolati / geofirestore-js

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

TypeError: Cannot read property 'enablePersistence' of undefined #51

Closed razbakov closed 5 years ago

razbakov commented 5 years ago

Versions

"firebase": "^5.6.0",
"geofirestore": "^2.2.3",

Code

import * as firebase from 'firebase/app';
import 'firebase/firestore';
import { GeoFirestore } from 'geofirestore'

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

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

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

// Create a GeoCollection reference
const geocollection = geofirestore.collection('events')

// Create a GeoQuery based on a location
const query = geocollection.near({ center: new firestore.GeoPoint(48.1077212, 11.6019103), radius: 50 })

// Get query (as Promise)
query.get().then((value) => {
  console.log(value.docs) // All docs returned by GeoQuery
})

Console error

Uncaught TypeError: Cannot read property 'enablePersistence' of undefined
    at new e (geofirestore.js?5cdc:1)

Example

https://codesandbox.io/s/y04wj6zpoj

MichaelSolati commented 5 years ago

I'm assuming you're testing version 3 of the library. With that in mind I don't believe your import syntax for for firebase is correct. Also you need to instantiate a firestore object, which I don't see.

I see some similarities in what you have to the sample I have in the readme, please give it another look though https://github.com/geofirestore/geofirestore-js/blob/v3.0.0/README.md#documentation

And if you have any questions let me know. I'll keep the issue open till Sunday so you can let me know if that works.

razbakov commented 5 years ago

I updated the code, basically I have the part of the code of initialising app and firestore in a separate file.

MichaelSolati commented 5 years ago

@razbakov what version are you using? (And would you possibly be cool with making sample of on something like https://codepen.io/ ??)

razbakov commented 5 years ago

My versions are:

"firebase": "^5.6.0",
"geofirestore": "^2.2.3",
razbakov commented 5 years ago

Here is example - https://codesandbox.io/s/y04wj6zpoj

MichaelSolati commented 5 years ago

Ok! This I can easily address. I think you may have been referencing the documentation for version 3 which is located on geofirestore.com, however you're using version 2.x.x

Check out the docs for version 2

And as an example on how to do what you wanted to do...

import * as firebase from 'firebase/app';
import 'firebase/firestore';
import { GeoFirestore } from 'geofirestore'

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

// Array to store events
let events = [];

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

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

// Create a GeoQuery based on a location
const query = geofirestore.query({ center: new firebase.firestore.GeoPoint(48.1077212, 11.6019103), radius: 50 })

// Listener for docs as they come in
query.on('key_entered', ($key, result) => {
    result.$key = $key;
    events.push(result);
});

// Listener for docs as they exit
query.on('key_exited', ($key) => {
    events = events.filter((event) => event.$key !== $key));
});

IF however you want to use version 3.0.0 (as your current code shows), you can install it like this:

npm i geofirestore/geofirestore-js#v3.0.0

It's still in kind of a beta state, however it's almost good to go, and I feel comfortable saying you could/should try it. I'm just writing tests to validate it works, otherwise the development is pretty much done (unless a test tells me I screwed up)