【问题标题】:Using Fetch with Google Places API将 Fetch 与 Google Places API 结合使用
【发布时间】:2019-02-04 02:18:17
【问题描述】:

您好,这可能是一个非常新手的错误,但有谁知道为什么 console.logging 未定义?当我将 url 提交到浏览器时,我得到了有效的 json 响应。谢谢

      fetch('https://maps.googleapis.com/maps/api/place/nearbysearch/json?key=[API KEY HERE]&location=49.246292,-123.116226&radius=500000')
        .then((resp) => {
          resp.json();
        })
        .then((data) => {
          console.log(data);
        })
        .catch(err => console.error(err));

【问题讨论】:

  • 您是否在实际测试中放入了 API 密钥?

标签: javascript google-places-api fetch-api


【解决方案1】:

因为您决定将箭头函数体包裹在花括号中,所以您需要专门返回数据:

.then((resp) => {
  return resp.json();
})

或者删除花括号:

.then(resp => resp.json())

【讨论】:

  • K 我明白了......所以它没有从承诺中返回任何东西......干杯
猜你喜欢
  • 2018-04-25
  • 2016-04-02
  • 2014-02-18
  • 1970-01-01
  • 1970-01-01
  • 2012-09-12
  • 2013-04-12
  • 2014-03-18
  • 1970-01-01
相关资源
最近更新 更多