首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何为Google地图中的placeid获取axios数据并作出反应?

如何为Google地图中的placeid获取axios数据并作出反应?
EN

Stack Overflow用户
提问于 2018-06-10 10:49:13
回答 1查看 10.2K关注 0票数 1

我在状态restaurantsFetchedFromGoogle中有一个数组,该数组目前存储20个以前从谷歌地图中获取的placeids。它们以这样的方式存储在数组中:

代码语言:javascript
复制
0:"ChIJTb1qU315MhMRiL7pihnMWfg"
1:"ChIJGZ0jXCCNMRMRrH8VHRAgEYE"
2:"ChIJQ5jaX8eMMRMRnRoZxl6X95w"
...
19:"ChIJQ5jaX8errrMRnRoZxl6X95w"

现在,我尝试使用Axios请求迭代数组,如:

代码语言:javascript
复制
componentDidMount(){
    navigator.geolocation.getCurrentPosition(position=>{
    this.setState({geoLat:position.coords.latitude,geoLng:position.coords.longitude});
    axios.get(`https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=${position.coords.latitude},${position.coords.longitude}&radius=50000&keyword=recensioni&type=ristorante&keyword=cena&key=MYAPIKEY`)
         .then(response => this.setState({restaurantsFetchedFromGoogle:response.data.results}));
    });
    this.state.restaurantsFetchedFromGoogle.map(item=>{
      axios.get(`https://maps.googleapis.com/maps/api/place/details/json?placeid=${item.place_id}&key=AIzaSyCZ7rgMN34kWkGvr8Pzkf_8nkT7W6gowBA`)
           .then(response => this.setState({placeId:response.data}))
           .catch(err =>{
           console.log(err);
           console.log(err.response.data.error);
    });
  });
    axios.get('/restaurants.json')
         .then(response =>this.setState({restaurants:response.data}));
  }

第一个和第三个axios请求通过,但不是第二个axios请求。我做错了什么?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-10 11:00:10

添加一个错误处理程序来记录错误消息!这应该能告诉你为什么请求失败了。

代码语言:javascript
复制
this.state.restaurantsFetchedFromGoogle.map(item=>{
axios.get('https://maps.googleapis.com/maps/api/place/details/json?placeid='+ 
item + '&key=MYAPIKEY')
   .then(response => this.setState({placeId:response.data}))
   .catch(err => {
     console.log(err)                     //Axios entire error message
     console.log(err.response.data.error) //Google API error message 
   })
});

即使没有.catch -您也可以使用chrome工具来查看开发工具中的Network下的错误。

如果您在发布具体错误消息后仍然有问题,我会查看。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50782908

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档