0

My code does not work. I need to compare the value of the array to the value that was entered and return the latitude and longitude values in order to plot this point on the map, but only get the error Cannot read property Latitude of undefined.

var auto1 = [{"Latitude": 42.389, "Longitude": -8.567, title: "test"}];

var digit = document.getElementById('searchmap');
var ret = digit.value;

function search(namek, myarray){
   for(var i=0; i<myarray.length; i++){
      if(myarray[i].title === namek){
         return myarray[i];
       } else{ return false;
       }
    }
 }

var obj = search(ret,auto1);
function showauto(){
     var retu = new google.maps.LatLng(obj.Latitude,obj.Longitude);
     map.setCenter(retu);
 }

I want map.setCenter(retu) works. Thanks

  • 1
    You should not assume that `search` always returns some valid object. – smac89 Nov 06 '19 at 23:41
  • Possible duplicate of [JavaScript "cannot read property "bar" of undefined](https://stackoverflow.com/questions/8004617/javascript-cannot-read-property-bar-of-undefined) – smac89 Nov 06 '19 at 23:41
  • If `obj` is `false`, it won't have `.Latitude` and `.Longitude` properties. – geocodezip Nov 07 '19 at 11:39

2 Answers2

1

Your search function has a conditional return value. So in the case that none of the items in the array argument match the condition, it will return undefined. You need to handle this case before trying to reference properties on the function's return value.

Edit: I'm hesitant to provide a working example because I have no idea what your spec is, but here's one that would work:

function showauto() {
  const obj = search(ret, auto1)
  if (obj) {
    var retu = new google.maps.LatLng(obj.Latitude,obj.Longitude)
    map.setCenter(retu)
  }
}

In the case that search returns a falsy value, the showauto function will not attempt to run the maps logic.

djfdev
  • 5,747
  • 3
  • 19
  • 38
  • That's not really what I was suggesting. I was suggesting that you check the return value of `search` before trying to use it. Returning `false` will yield the exact same problem as you already have. If the return value of the function is not an object, then you shouldn't attempt to treat it like one. – djfdev Nov 07 '19 at 00:10
  • @user10381466 what is `namek, myarray` can you show the updated code? – Zachary Blumstein Nov 07 '19 at 00:16
0

Try something like this:

function search(name, myarray) {
    const foundItem = myarray.find(element => {
        return element.title === name
    })
    return foundItem
}

Also I'm not quite sure what are you doing, but maybe you have error in that ret variable

var ret = digit.value

You are not providing correct value to the function search on line: var obj = search(ret,auto1) so your search function fails to find anything and it returns no value, and obj becomes undefined and you can't access property of undefined because there is none.

  • Thanks @ShoeShineBoy. You are right, ret variable have error. I changed var obj = search(ret,auto1) for var obj = search(digit.value,auto1). It's works for me – user10381466 Nov 08 '19 at 14:50