googlemaps / google-maps-services-js

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

Typescript error in the distance matrix api #365

Closed nanduri1998 closed 4 years ago

nanduri1998 commented 4 years ago

Says can't perform map

jpoehnelt commented 4 years ago

Please add some additional information such as the error message and reopen this when you do.

Here is an e2e test that you can compare your code against: https://github.com/googlemaps/google-maps-services-js/blob/master/e2e/distance.test.ts

nanduri1998 commented 4 years ago
TypeError: o.map is not a function
    at Object.origins (/srv/node_modules/@googlemaps/google-maps-services-js/dist/distance.js:18:21)
    at Object.keys.forEach (/srv/node_modules/@googlemaps/google-maps-services-js/dist/serialize.js:90:42)
    at Array.forEach (<anonymous>)
    at /srv/node_modules/@googlemaps/google-maps-services-js/dist/serialize.js:88:29
    at buildURL (/srv/node_modules/axios/lib/helpers/buildURL.js:31:24)
    at dispatchHttpRequest (/srv/node_modules/axios/lib/adapters/http.js:84:13)
    at new Promise (<anonymous>)
    at httpAdapter (/srv/node_modules/axios/lib/adapters/http.js:21:10)
    at dispatchRequest (/srv/node_modules/axios/lib/core/dispatchRequest.js:52:10)
    at <anonymous>

This is the error I get, Deployed it using this lib on Firebase Cloud Functions.

My input is the following: { origin: 'burjuman', destination: 'ikea dubai' }

My code for this:

var origin = req.body.origin;
    var destination = req.body.destination;
    client.distancematrix({
        params: {
            origins: origin,
            destinations: destination,
            key: functions.config().google.key
        }
    }).then(data => {
        console.log(data.data);
        res.json(data.data);
    }).catch(err => console.log(err));

client is the Google Maps constructor, The above code is executed in a Express App POST Call with input provided above (Removed redundant body check code and post params and function initiation).

jpoehnelt commented 4 years ago

The param should be an array.

origins: [origin],
destinations: [destination],

https://github.com/googlemaps/google-maps-services-js/blob/master/src/distance.ts#L48

nanduri1998 commented 4 years ago

The param should be an array.

origins: [origin],
destinations: [destination],

https://github.com/googlemaps/google-maps-services-js/blob/master/src/distance.ts#L48

Got it. But please do update any documentation as it is very confusing.