Cloud function response type error

Custom Code

I see such error in console

Error debug serializing parameter: TypeError: Instance of 'GetNearbyRestaurantsCloudFunctionCallResponse': type 'GetNearbyRestaurantsCloudFunctionCallResponse' is not a subtype of type 'String?'

+ I see such thing in action output

(it's even have a wrong type icon)


I have created an cloud function

const functions = require('firebase-functions');
const admin = require('firebase-admin');
// To avoid deployment errors, do not call admin.initializeApp() in your code

/**
 * Calculates the distance between two points (latitude/longitude)
 * using the Haversine formula and returns the result in miles.
 */
function getDistanceMiles(lat1, lon1, lat2, lon2) {
  const toRad = x => x * Math.PI / 180;
  const R = 6371; // Earth’s radius in kilometers
  const dLat = toRad(lat2 - lat1);
  const dLon = toRad(lon2 - lon1);

  const a =
    Math.sin(dLat / 2) * Math.sin(dLat / 2) +
    Math.cos(toRad(lat1)) * Math.cos(toRad(lat2)) *
    Math.sin(dLon / 2) * Math.sin(dLon / 2);

  const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
  const distKm = R * c;

  return distKm * 0.621371; // convert kilometers to miles
}

exports.getNearbyRestaurants = functions
  .runWith({
    timeoutSeconds: 20,
    memory: '1GB'
  })
  .https.onCall(async (data, context) => {
    // 1) Parse the incoming location
    let userLat, userLon;
    const loc = data.location;

    console.log('getNearbyRestaurants called with:', data);

    if (typeof loc === 'string') {
      // Expect format: "LatLng(lat: 25.7813487, lng: -80.2879732)"
      const match = loc.match(/LatLng\(\s*lat:\s*([-\d.]+),\s*lng:\s*([-\d.]+)\s*\)/);
      if (!match) {
        throw new functions.https.HttpsError(
          'invalid-argument',
          'currentDeviceLocation string must be in format "LatLng(lat: <number>, lng: <number>)".'
        );
      }
      userLat = parseFloat(match[1]);
      userLon = parseFloat(match[2]);
    } else if (
      loc != null &&
      typeof loc.latitude === 'number' &&
      typeof loc.longitude === 'number'
    ) {
      // Object format
      userLat = loc.latitude;
      userLon = loc.longitude;
    } else {
      throw new functions.https.HttpsError(
        'invalid-argument',
        'The function must be called with "currentDeviceLocation" as a string or object.'
      );
    }


    console.log('LOOOOOC', userLat, userLon);

    // 2) Load all restaurants from Firestore
    const restaurantsSnap = await admin.firestore()
      .collection('restaurants')
      .get();

    const ONE_KM_IN_MILES = 0.621371;


    console.log('REEEEST', restaurantsSnap);

    // 3) For each restaurant, fetch its locationDetails and compute distance
    const withDistance = await Promise.all(
      restaurantsSnap.docs.map(async doc => {
        const base = doc.data();
        const locSnap = await doc.ref
          .collection('locationDetails')
          .limit(1)
          .get();

        if (locSnap.empty) {
          return null; // no location, skip
        }

        const details = locSnap.docs[0].data();
        const distanceMiles = getDistanceMiles(
          userLat,
          userLon,
          details.lat,
          details.lon
        );

        return {
          name: base.name,
          category: base.category,
          cuisine: base.cuisine,
          opening_hours: details.opening_hours,
          phone: details.phone,
          website: details.website,
          addrcity: details['addr:city'],
          addrhousenumber: details['addr:housenumber'],
          addrpostcode: details['addr:postcode'],
          addrstate: details['addr:state'],
          addrstreet: details['addr:street'],
          lat: details.lat,
          lon: details.lon,
          distanceMiles
        };
      })
    );

    // 4) Filter out nulls, then by radius and sort
    const nearby = withDistance
      .filter(r => r && r.distanceMiles <= ONE_KM_IN_MILES)
      .sort((a, b) => a.distanceMiles - b.distanceMiles);

    // 5) Return the result
    return { nearby };
  });



besides I see in network that requet is successfull

Please help me to find out what is the issue? I don't understnad

What have you tried so far?

I've tried to clear cloud function custom code and write only

return "test"

same result.

I've also changed return value to DataType and tried both "is List" - false and true

same result.

Did you check FlutterFlow's Documentation for this topic?
Yes
3
1 reply