Search code examples
firebasegoogle-cloud-functionsgeofirestore

How to implement GeoFirestore in Firebase Cloud-Functions the right way?


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"
},

Solution

  • 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.