Skip to content

Commit

Permalink
[Fix] Cannot read properties of undefined (reading 'country_code') (#354
Browse files Browse the repository at this point in the history
)
  • Loading branch information
nrathi authored Mar 17, 2024
1 parent f6fecdc commit e225f77
Showing 1 changed file with 25 additions and 24 deletions.
49 changes: 25 additions & 24 deletions lib/geocoder/openstreetmapgeocoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,34 +67,35 @@ OpenStreetMapGeocoder.prototype._geocode = function(value, callback) {
};

OpenStreetMapGeocoder.prototype._formatResult = function(result) {

var countryCode = result.address.country_code;
if (countryCode) {
countryCode = countryCode.toUpperCase();
}

var latitude = result.lat;
if (latitude) {
latitude = parseFloat(latitude);
var countryCode, latitude, longitude, formattedAddress, country, city, state, zipcode, streetName, streetNumber, neighbourhood;

if (result.address) {
countryCode = result.address.country_code ? result.address.country_code.toUpperCase() : undefined;
country = result.address.country;
city = result.address.city || result.address.town || result.address.village || result.address.hamlet;
state = result.address.state;
zipcode = result.address.postcode;
streetName = result.address.road || result.address.cycleway;
streetNumber = result.address.house_number;
neighbourhood = result.address.neighbourhood || '';
}

var longitude = result.lon;
if (longitude) {
longitude = parseFloat(longitude);
}
latitude = result.lat ? parseFloat(result.lat) : undefined;
longitude = result.lon ? parseFloat(result.lon) : undefined;
formattedAddress = result.display_name;

return {
'latitude' : latitude,
'longitude' : longitude,
'formattedAddress': result.display_name,
'country' : result.address.country,
'city' : result.address.city || result.address.town || result.address.village || result.address.hamlet,
'state': result.address.state,
'zipcode' : result.address.postcode,
'streetName': result.address.road || result.address.cycleway,
'streetNumber' : result.address.house_number,
'countryCode' : countryCode,
'neighbourhood': result.address.neighbourhood || ''
'latitude': latitude,
'longitude': longitude,
'formattedAddress': formattedAddress,
'country': country,
'city': city,
'state': state,
'zipcode': zipcode,
'streetName': streetName,
'streetNumber': streetNumber,
'countryCode': countryCode,
'neighbourhood': neighbourhood
};
};

Expand Down

0 comments on commit e225f77

Please sign in to comment.