nchaulet / node-geocoder

nodejs geocoding library
http://nchaulet.github.io/node-geocoder/
MIT License
930 stars 214 forks source link

Question: Do I need to escape the strings I pass into .geocode()? #237

Closed samhagman closed 7 years ago

samhagman commented 7 years ago

In your example you say you can just pass in an unescaped string:

geocoder.geocode('29 champs elysée paris', function(err, res) {
  console.log(res);
});

If I try to pass in a string with any spaces in it at all I get this error:

TypeError: Request path contains unescaped characters
    at new ClientRequest (_http_client.js:125:13)
    at Object.request (http.js:39:10)
    at Object.request (https.js:239:15)
    at HttpsAdapter.HttpAdapter.get (/usr/src/app/node_modules/node-geocoder/lib/httpadapter/httpadapter.js:48:27)
    at GoogleGeocoder._geocode (/usr/src/app/node_modules/node-geocoder/lib/geocoder/googlegeocoder.js:76:20)
    at GoogleGeocoder.AbstractGeocoder.geocode (/usr/src/app/node_modules/node-geocoder/lib/geocoder/abstractgeocoder.js:76:15)
    at Geocoder.<anonymous> (/usr/src/app/node_modules/node-geocoder/lib/geocoder.js:25:24)
    at Geocoder.<anonymous> (/usr/src/app/node_modules/node-geocoder/lib/geocoder.js:24:23)
    at Geocoder.<anonymous> (/usr/src/app/node_modules/continuation-local-storage/context.js:84:17)

Any help would be much appreciated!

nchaulet commented 7 years ago

No you should not, seems like a bug which version of the module, and node are you using?

nchaulet commented 7 years ago

With the latest version seems to work

const NodeGeocoder = require('node-geocoder');
const geocoder = NodeGeocoder({
  provider: 'google'
})

geocoder.geocode('29 champs elysée paris', function(err, res) {
  console.log(res, err);
});
// output
[ { formattedAddress: '29 Av. des Champs-Élysées, 75008 Paris, France',
    latitude: 48.869384,
    longitude: 2.3071868,
    extra:
     { googlePlaceId: 'ChIJ4e9zicRv5kcRi_EQo6FKduM',
       confidence: 1,
       premise: null,
       subpremise: null,
       neighborhood: 'Paris',
       establishment: null },
    administrativeLevels:
     { level2long: 'Paris',
       level2short: 'Paris',
       level1long: 'Île-de-France',
       level1short: 'Île-de-France' },
    streetNumber: '29',
    streetName: 'Avenue des Champs-Élysées',
    city: 'Paris',
    country: 'France',
    countryCode: 'FR',
    zipcode: '75008',
    provider: 'google' } ] null
samhagman commented 7 years ago

Hey! So I had checked our package.json version and it was the latest but our yarn lockfile had an older version. So updated to the latest version and everything is now working as it should! False alarm, thank you for the response.