Node.js调用Google Maps Distance Matrix API报错:"o.map is not a function"
解决Google Maps API Node Client调用distancematrix时的"o.map is not a function"错误
错误原因
问题出在origins和destinations的参数格式上。客户端源码里会对这两个参数调用数组的map方法,但你传入的是字符串类型,字符串没有map方法,所以触发了这个错误。
修复代码
把origins和destinations改成数组格式,哪怕只有单个地点也要用数组包裹,同时补上构造函数的括号(原代码new Client缺了括号,属于不规范写法):
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) })
补充说明
这个Node客户端要求origins和destinations必须是数组类型,数组元素可以是经纬度字符串、地址字符串或者Place ID,客户端会自动处理数组内容,转换成API要求的请求格式。
内容的提问来源于stack exchange,提问作者Brian Zhou
相关产品推荐
相关产品推荐

