googlemaps / google-maps-services-js

Node.js client library for Google Maps API Web Services
Apache License 2.0
2.9k stars 639 forks source link

PlacesAutocomplete call crashes if the optional location field is present but undefined #1052

Open Jiia opened 1 year ago

Jiia commented 1 year ago

PlacesAutocomplete call crashes if the optional params.location field is present but undefined.

Environment details

OS: macOS Ventura 13.4 Library version: 3.3.41

Steps to reproduce

Call placesAutocomplete with { params: { ...rest, location: undefined } }

Code example

const response = await new Client({}).placeAutocomplete({
  params: {
    key: apiKey,
    input,
    radius: 50000,
    location: undefined
  },
})

Stack trace

Error: Cannot use 'in' operator to search for 'lat' in undefined
at dispatchHttpRequest (/app/node_modules/@googlemaps/google-maps-services-js/node_modules/axios/dist/node/axios.cjs:2797:25)
at /app/node_modules/@googlemaps/google-maps-services-js/node_modules/axios/dist/node/axios.cjs:2568:5
at new Promise (<anonymous>)
at wrapAsync (/app/node_modules/@googlemaps/google-maps-services-js/node_modules/axios/dist/node/axios.cjs:2548:10)
at http (/app/node_modules/@googlemaps/google-maps-services-js/node_modules/axios/dist/node/axios.cjs:2574:10)
at /app/node_modules/@googlemaps/google-maps-services-js/dist/adapter.js:66:9
at new Promise (<anonymous>)
at /app/node_modules/@googlemaps/google-maps-services-js/dist/adapter.js:65:12
at dispatchRequest (/app/node_modules/axios/lib/core/dispatchRequest.js:58:10)
at Axios.request (/app/node_modules/axios/lib/core/Axios.js:109:15)

If the location is completely unspecified, the function call works fine. This can be solved by defining location like:

location: ...(myCoordinates ? { location: myCoordinates } : {}),
wangela commented 1 year ago

If you would like to upvote the priority of this issue, please comment below or react on the original post above with :+1: so we can see what is popular when we triage.

@Jiia Thank you for opening this issue. 🙏 Please check out these other resources that might help you get to a resolution in the meantime:

This is an automated message, feel free to ignore.

usefulthink commented 1 year ago

This seems to be the reason:

https://github.com/googlemaps/google-maps-services-js/blob/44ed812c133cee94a12d399af902b4f4173356be/src/serialize.ts#L27-L45

Treating undefined and null like an empty string should be a good solution here.