I’m using the Google Maps API Node Client and calling the distancematrix
function, but I’m getting an error that says o.map is not a function
.
const {Client} = require("@googlemaps/google-maps-services-js")
const client = new Client
client.distancematrix({
params: {
origins: '40,40',
destinations: '40,41',
key: process.env.GOOGLE_MAPS_API_KEY
}
})
.then((r) => {
console.log(r.data.rows)
})
.catch((e) => {
console.log(e)
})
I tried looking at the Google Maps services code. I found o.map
at line 174 at this link.
2
Answers
Based on this given Google Maps service code, you have to pass the origins & destinations as an array of [lat, lang] values. As you are passing it as a
string
andstring
doesn’t have the function callmap()
, it is throwing an error as o.map is not a function. Try the below code and checkHappy coding!
The distancematrix function expects an array of origins and destinations, but in your code, you are passing a string for both. Instead, you should provide arrays of coordinates for origins and destinations.
Make sure to pass arrays for origins and destinations, even if you have only one origin and one destination.
If the issue persists, you may also want to check the version of the @googlemaps/google-maps-services-js package you are using and update it to the latest version to ensure that you have the latest bug fixes.