2017-10-15 80 views
0

财产“几何”我的代码工作像它应该,但我不断收到错误这个问题的标题我的移动设备上的同名每当这个函数被调用。无法读取的不确定

_matchAddresses(id) { 
    fetch('https://myapp.com/rest/jobs/address/' + id) 
    .then((response) => response.json()) 
    .then((responseJson) => { 
    var addresses = []; 
    console.log('addresses: ' + responseJson); 
    for (var i=0; i<responseJson.length; i++) { 
     addresses.push(responseJson[i]); 
     for (var j=0; j<addresses.length; j++) { 
     fetch('https://maps.googleapis.com/maps/api/geocode/json?address='+ addresses[j].split(" ") + '&key=googleapikey') 
     .then((response) => response.json()) 
     .then((responseJson) => { 
      let lat = responseJson.results["0"].geometry.location.lat; //error here 
      let lng = responseJson.results["0"].geometry.location.lng; //error here 
      console.log("coordinates: " + lat + " " + lng); //returns coordinates every time 

      if (this._latMatch(userLat, lat) && this._lngMatch(userLng, lng)) { 
       this._myFunction(); 
       console.log("fire"); 
      } 

     }) 
     .catch((error) => { 
      console.error(error); 
     }); 
    } 
    } 
}); 
} 

我知道,纬度和经度已与一些日志定义的,所以我dont't明白为什么我仍然会得到错误。先谢谢你。

回答

2

results["0"]是回来了空,这就是为什么你得到这个错误。既然你在一个迭代的for循环,你已成功打印出的坐标,我的猜测是,这些回迁到谷歌的API的至少一个没有返回预期的结果。为了测试这个,你可以在尝试访问它之前记录结果对象并检查错误发生之前返回的对象。

+0

谢谢您的帮助。事实证明,我的后端的一个地址有它一个错字,这是导致Google地图API搜索一个不存在的地址。很有趣的是我是如何得到不同坐标的正确数量虽然...如果我有足够的声誉我会给你一个了投票。 –

+0

很高兴能帮到你! –