javascriptfirebasegoogle-cloud-firestoregeopointsgeofirestore

Error: Invalid location: latitude must exist on GeoPoint


I am getting the below error when accessing firestore document in cloud function

"Error: Invalid location: latitude must exist on GeoPoint"

I am seeing below error on firebase logs

Error: Invalid location: latitude must exist on GeoPoint at validateLocation (/srv/node_modules/geofirestore/dist/index.cjs.js:567:15) at calculateDistance (/srv/node_modules/geofirestore/dist/index.cjs.js:120:5) at /srv/node_modules/geofirestore/dist/index.cjs.js:1148:32 at Array.forEach () at /srv/node_modules/geofirestore/dist/index.cjs.js:1147:27 at Array.forEach () at new GeoJoinerGet (/srv/node_modules/geofirestore/dist/index.cjs.js:1146:19) at /srv/node_modules/geofirestore/dist/index.cjs.js:1432:72 at at process._tickDomainCallback (internal/process/next_tick.js:229:7)

my index.ts file

import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';import * as geofire from 'geofirestore';

admin.initializeApp();

const db = admin.firestore();

const geofirestore = new geofire.GeoFirestore(db);
const geocollection = geofirestore.collection('posts');

export const sendPostToDevice = functions.firestore
.document('posts/{postId}')
.onCreate((snapshot, context) => {
const post = snapshot.get('content');

    const l = snapshot.get('l');

    const GeoPoint = admin.firestore.GeoPoint;

    const gp = new GeoPoint(37.4219983, -122.084);
    const query = geocollection.near({
        center: gp,
        radius: 1000
    });
    query.get().then((value) => {
        console.log(value);
    }).catch((err) => console.log(err));
    return null;
})

Additional Info: I am using GeoFlutterFire in my flutter app and storing the additional fields(g: geohash and l:location stored as geopoint), when creating the Post document, required by GeoFirestore. I suspect the error is because of how I am storing the fields 'g' and 'l'.

I am using GeoFirestore in Cloud Functions to query for nearby users, retrieve their FCM Tokens and provide them to Firebase Messaging to send notifications.

Do I need to store the fields g and l in a certain way? Can we use just GeoFlutterFire for my purpose?

Thank you for your help!

Fields in my firestore.


Solution

  • new GeoPoint(37.4219983, -122.084) looks okay so I suspect it must be one of your geocollection objects' geolocations that is causing trouble.