0

I am creating a script to find locations starting from UK Postcodes using the Here Maps Geocoder Javascript API. The code seems to work fine, but on the postcode BT45 7PX I got the error

TypeError: result.Response.View[0] is undefined

The code I am using is as follows:

var platform = 0;
var postcode = 0; /*random value*/

$(document).ready(function(){
    platform = new H.service.Platform({
        app_id: '(myapp_id)',
        app_code: '(myapp_code)',
        useCIT: true
        });

    document.getElementById('searchInput').value.toUpperCase();
    postcode = contentRead.replace(/\s+/g, '');
    geocode(postcode);
})

function geocode(postcode){
    var geocoder = platform.getGeocodingService(),
    geocodingParameters = {
        searchText: postcode
    };

    geocoder.geocode(
        geocodingParameters,
        onSuccess,
        onError
        );
}


function onSuccess(result){
    var location = result.Response.View[0].Result[0];
    var city = location.Location.Address.City;
    var lat = location.Location.DisplayPosition.Latitude;
    var lng = location.Location.DisplayPosition.Longitude;
    console.log('location: ');
    console.log(location);
    console.log('latitude: '+lat);
    console.log('longitude: '+lng);
    $('#rightResult').append('<div>The postcode searched points to... <strong>'+city+'</strong></div>');
}


function onError(error){
    alert('Ooops, something went wrong, please try again!');
}

Any clue about why the code doesn't work properly with BT45 7PX?

Trying with a basic JSON call (http://geocoder.api.here.com/6.2/geocode.json?app_id=(myapp_id)&app_code=(myuapp_code)&searchtext=BT45%207PX) I found that the call works if I leave the space between, but removing it, as I was doing, it doesn't anymore. This doesn't happen with other postcodes.

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
tabris963
  • 110
  • 2
  • 14
  • According to the [Royal Mail website](http://www.royalmail.com/sites/default/files/docs/pdf/programmers_guide_edition_7_v5.pdf) there **should** always be a space between between the out code and the in code. The second part - i.e. the in code should **always** be of the form `space-number-alpha-alpha`. The JavaScript found on [this site](http://www.braemoor.co.uk/software/postcodes.shtml) validates and adds a space. A basic regex for the in code is `([0-9]{1}[ABDEFGHJLNPQRSTUWXYZ]{2})$` – Jason Fox Apr 13 '15 at 14:38
  • Thank you, as I said to Jithin by now I solved passing always a postcode with a space. However, because you linked that regex, I was using this other one: http://stackoverflow.com/a/164994/3717575 – tabris963 Apr 13 '15 at 15:55

2 Answers2

0

We will be able to help you better if you post the API response, but given your edit, it looks like removing the space simply causes the API to not return a result. Because of this you are trying to access an array index that simply doesn't exist. At some point in your code you will probably want to handle for this scenario, so now would be a good time to do that. Post the response (without a valid result) and I will help you figure out what to use to determine wether a proper result was found or not.

rdgd
  • 1,451
  • 2
  • 18
  • 33
0

It is always good to have null checks when processing a response, something like the following should work

if(response && response.Response && response.Response.View[0] && response.Response.View[0].Result[0]) {
    // Process Result
}

Regarding the postal code with spaces, this seems to be an issue with postal codes in UK.

Jithin Krishnan
  • 1,044
  • 1
  • 6
  • 6
  • It appears to be specific to **Belfast** BTnn postcodes, **Birmingham** works fine like [B338TH](https://geocoder.cit.api.here.com/6.2/geocode.json?postalCode=B338TH&country=gbr&gen=6&app_id=DemoAppId01082013GAL&app_code=AJKnXv84fjrb0KIHawS0Tg). According to the [Royal Mail website](http://www.royalmail.com/sites/default/files/docs/pdf/programmers_guide_edition_7_v5.pdf) there **should** always be a space between between the out code and the in code. – Jason Fox Apr 13 '15 at 14:28
  • The null check was on my 'to do' list, but I wasn't expecting such an issue there. By now to keep going with my project I added a control to pass always a postcode with spaces. However I still don't get why it should generate that response only with some postcodes and not with all of them... – tabris963 Apr 13 '15 at 15:52