node-where icon indicating copy to clipboard operation
node-where copied to clipboard

Cannot read property 'address_components' of undefined

Open bit-crafter opened this issue 8 years ago • 3 comments

Sometimes body parameter is undefined and it's caused exception in _resultAddressComponent function line 1.

bit-crafter avatar Sep 29 '17 07:09 bit-crafter

Unless this is addressed/explained, this causes a fatal application exception and becomes unusable.

theflyingape avatar Feb 17 '18 16:02 theflyingape

It looks like there are no validations on the address.js file for "null pointer" exceptions. So, for example, the method _resultAddressComponent tries to access the parameter body.address_components without checking if body is undefined. This causes the code to crash.

The method that calls resultAddressComponent has to be changed as well since it also has some null exceptions. except

PauloGasparSv avatar Feb 27 '18 19:02 PauloGasparSv

Besides the error in function _resultAddressComponent(body, componentName)

you may need to fix the _.extend(Address.prototype, { buildResult: function(body) {

The fix may look like this:

`_.extend(Address.prototype, { buildResult: function(body) {

body = body || {};



if (body && body.results) {
  body = body.results[0];
} 

var country = _resultAddressComponent(body, 'country');
var postalCode = _resultAddressComponent(body, 'postal_code');
var region = _resultAddressComponent(body, 'administrative_area_level_1');
var city = _resultAddressComponent(body, 'locality');
var neighborhood = _resultAddressComponent(body, 'neighborhood');
var streetNumber = _resultAddressComponent(body, 'street_number');
var street = _resultAddressComponent(body, 'route');
var county = _resultAddressComponent(body, 'administrative_area_level_2');

 //Added code for no body
var lat;
var lng;
if (body && body.geometry && body.location) {
  lat = body.geometry.location.lat;
  lng = body.geometry.location.lng;
}

return new Result({
  countryCode: country.short_name,
  country: country.long_name,
  regionCode: region.short_name,
  region: region.long_name,
  city: city.long_name,
  postalCode: postalCode.long_name,
   //Changed code for no body
  lat: lat,
  lng: lng,

  streetNumber: streetNumber.long_name,
  street: street.long_name,
  neighborhood: neighborhood.long_name,
  county: county.long_name
});

},

});

function _resultAddressComponent(body, componentName) { //Added code for no body body = body || {};

var components = body.address_components || {};

return _.find(components, function(component) { return _.includes(component.types, componentName); }) || {}; } `

nikolayds avatar Sep 03 '19 22:09 nikolayds