2

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.

RL Shyam
  • 109
  • 1
  • 2
  • 12
  • 1
    `new GeoPoint(37.4219983, -122.084)` looks okay so I suspect it must be one of your `geocollection` objects' geolocations that are causing trouble. Can you dump a few of them here? – Joe - GMapsBook.com Apr 04 '20 at 18:11
  • @jzzfs please see the image i just added. Thank you! – RL Shyam Apr 04 '20 at 23:22
  • @jzzfs hi, the query is working fine on a new collection posts. the error could be because of the absence of "l" field or storing "l" field as an array of latitude and longitude on some of the earlier post documents. thank you for looking it to this. – RL Shyam Apr 05 '20 at 00:35
  • @jzzfs it occurred to me that one of the earlier documents could be causing the error only after reading you comment. was clueless for almost 2 days. thanks for your help! – RL Shyam Apr 05 '20 at 00:45
  • cool! lemme post an answer too then to get the bounty lol – Joe - GMapsBook.com Apr 05 '20 at 01:19

1 Answers1

1

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

Joe - GMapsBook.com
  • 15,787
  • 4
  • 23
  • 68