Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Jiia opened this issue Oct 12, 2023 · 2 comments
Labels
type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns.

Comments

@Jiia
Copy link

Jiia commented Oct 12, 2023

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 } : {}),
@Jiia Jiia added triage me I really want to be triaged. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns. labels Oct 12, 2023
@wangela
Copy link
Member

wangela commented Oct 12, 2023

If you would like to upvote the priority of this issue, please comment below or react on the original post above with 👍 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 usefulthink removed the triage me I really want to be triaged. label Oct 12, 2023
@usefulthink
Copy link
Contributor

This seems to be the reason:

export function latLngToString(o: LatLng) {
if (typeof o === "string") {
return o;
} else if (Array.isArray(o) && o.length === 2) {
// no transformation
} else if ("lat" in o && "lng" in o) {
o = [o.lat, o.lng];
} else if ("latitude" in o && "longitude" in o) {
o = [o.latitude, o.longitude];
} else {
throw new TypeError();
}
return o
.map((x) => {
return x.toString();
})
.join(",");
}

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns.
Projects
None yet
Development

No branches or pull requests

3 participants