3

I try to implement GeoFirestore to my Firebase Cloud-Functions.

When I try to deploy my code this error occur "TypeError: GeoFire is not a constructor".

I use the index.js file for my functions.

I tried to implement the GeoFire reference in my function, but this won't help either ...

I tried everything but im not able to fix this problem. I hope you guys can help me :)

PS.: I change my Firebase DatabaseURL with XXXXXXX for this posts.

I searched the web and stack overflow for answers, but none of the answers fixed my problem.

My index.js looks like this:

const functions = require('firebase-functions');
var admin = require('firebase-admin');
var serviceAccount = require('./serviceAccountKey.json');
var GeoFire = require('geofirestore');

admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: "https://XXXXXXXXX.firebaseio.com"
});

admin.firestore().settings({
timestampsInSnapshots: true
});

const database = admin.firestore();
const Geo = new GeoFire(database.collection('UserLocations'));

My package.json dependencies looks like this:

"dependencies": {
"firebase-admin": "~6.0.0",
"firebase-functions": "^2.1.0",
"geofirestore": "^3.1.0"
},
Scholte47
  • 55
  • 4

1 Answers1

8

since you're using require syntax rather than import you will need to bring in individual classes like so...

var GeoFirestore = require('geofirestore').GeoFirestore;

Also, a geofirestore instance takes a firestore instance. So you wouldn't pass in the collection like you did in your code. You'd want to do this:

const database = admin.firestore();
const geofirestore = new GeoFirestore(database);
const Geo = geofirestore.collection('UserLocations');

Hope this helps.

MichaelSolati
  • 2,847
  • 1
  • 17
  • 29
  • Where is "geofirestore.initializeApp(admin.firestore())" ? Shouldn't you initialize it first? I mean that's what I do client-side but shouldn't that be the same server-side? – showtime Mar 05 '22 at 16:56
  • The first line initializes the firestore db, we then pass it to geofirestore in line 2. `initializeApp` isn't required and just exists to make the initialization feel more like regular firestore. But it is completely optional and does the same thing as above. – MichaelSolati Mar 06 '22 at 17:21