typescriptfirebasegoogle-cloud-firestoregoogle-cloud-functionsgeofirestore

geofirestore not returning individual document data


We are using the geofirestore npm package to get nearby documents.

This is the Cloud Function we are using to get these documents.

exports.getNearbyOffers = functions.https.onCall((data, context) => {
    const offers = GeoFirestore.collection('restaurants');

    const center : GeoPoint = new admin.firestore.GeoPoint(data["sourceLat"], data["sourceLng"]);

    const query = offers.near({center: center, radius: 100,});

    return query.get().then((value) => {
        console.log(value.docs);
        return value.docs;
    }).catch((err: any) => {
        console.log(err);
    });
});

The output on the client-side is:

[{data: {}, distance: 0, exists: true, id: LdaLz30gv16Gv68Xw92i}, {data: {}, distance: 0, exists: true, id: Ux1Et5afklvZC4IKEkJy}, {data: {}, distance: 0, exists: true, id: h4kdFZpsn1UwPpTvPMxC}, {data: {}, distance: 0, exists: true, id: oqWfc41cfiWAcnvufVFd}, {data: {}, distance: 0, exists: true, id: pH0qW0V9rY43zvgkPQm8}]

The query is returning the correct document IDs. However, the data of these documents is empty, but, in the Firestore console, each corresponding document has many fields with data (name, rating, address, etc.).


Solution

  • That's expected because you are getting only the snapshot. you need to run the data() function on each doc to get their data:

    query.get().then((value) => {
    // All GeoDocument returned by GeoQuery, like the GeoDocument added above
      for (const doc of value.docs) {
        console.log(doc.data());
      }