node.jsfirebasegeofirestore

Problem with the set() function from GeoFirestore


I want to use the set(...) function, but Firebase-Log says that this function is not available.

The Firebase-Cloud-Function Log:

"TypeError: GeoPostLocations.set is not a function at database.collection.doc.collection.doc.set.then.result"

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

var GeoFirestore = require('geofirestore').GeoFirestore;
const database = admin.firestore();
const geofirestore = new GeoFirestore(database);
const GeoPostLocations = geofirestore.collection('PostLocations');

In my function, I execute the following code:

return GeoPostLocations.set("DummyIDForTest", [50.312312312, 5.4302434234]]).then(result => {
        console.log(result);
        return 0;
    }).catch(error => {
        console.log(error);
        return 1;
    });           

Solution

  • So it looks like you've been doing a bit of your code in the syntax of version 2 of geofirestore, but we're now on v3 and things should look more like firestore, so what does that mean for you?

    Firstly, set isn't a function available to collections, but it is available to documents, so you'll wanna do this:

    return GeoPostLocations.doc('DummyIDForTest').set({
        coordinates: new firebase.firestore.GeoPoint(50.312312312, 5.4302434234)
    }).then(result => {
        console.log(result);
        return 0;
    }).catch(error => {
        console.log(error);
        return 1;
    });
    

    Please note that you have to set an object, not coordinates in an array (this isn't geofire). I've included a link to the relevant docs.