【问题标题】:Awaiting coordinates from geocoding google maps API returns pending promise等待来自地理编码谷歌地图 API 的坐标返回待处理的承诺
【发布时间】:2019-11-25 12:44:43
【问题描述】:

我正在尝试使用谷歌地图地理编码 API 和获取返回给定地址的坐标。我可以在我的 get 函数中记录这些坐标,但我不知道如何从函数中返回它们以在代码中的其他地方使用它。已经尝试过两种方法的多种变体:

function getCoordinates1(name) {
  locObj =  fetch(`https://maps.googleapis.com/maps/api/geocode/json?address=${name}&key=mykey`).then( (res) => res.json()).then( (data) => 
  { 
    console.log(data.results[0].geometry.location);
    return data.results[0].geometry.location;
  }).then((res) => res);
  }

let coordinates1 = getCoordinates1(latinaze(name2));
console.log(coordinates1);


async function getCoordinates2(name) {
  locObj =  await fetch(`https://maps.googleapis.com/maps/api/geocode/json?address=${name}&key=mykeyk`).then( (res) => res.json()).then( (data) => 
  { 
    console.log(data.results[0].geometry.location);
    //return data.results[0].geometry.location;
  }).then((res) => res);
  return locObj
  }

let coordinates2 = await getCoordinates2(latinaze(name2));
console.log(coordinates2);

第一个函数返回未定义,第二个返回待处理的承诺。我做错了什么?

【问题讨论】:

    标签: node.js google-geocoding-api


    【解决方案1】:

    第一个函数返回undefined,因为你没有返回任何东西。就这么简单;)


    第二个函数返回一个待处理的承诺,因为您不需要等待承诺被解决。当调用 then 内的回调时,承诺会得到解决,但这会发生在 你在 getCoordinates2 中返回 locObj 之后。


    你应该试试这个:

    // function definition
    async function getCoordinates3(name) {
      const resp = await fetch(`https://maps.googleapis.com/maps/api/geocode/json?address=${name}&key=mykeyk`)
      const data = await resp.json();
      return data.results[0].geometry.location;
    }
    
    // usage
    const coordinates3 = await getCoordinates3(latinaze(name3));
    

    【讨论】:

    • 一开始我得到了一个错误,await can't be used outside the async function。当我将用法包装在另一个异步函数中时,出现类型错误:无法读取未定义的属性“几何”
    • 您能否在分配data 时检查它的值并告诉我它是什么?
    • 实际上我用不同的方法重建了整个代码,它现在可以工作了。谢谢你的回答,我真的很感激;)。如果以后有人遇到同样的问题,我会在一段时间内回答自己。
    【解决方案2】:

    我无法从函数返回任何值,所以我将其设为类方法并在函数体中设置属性。现在我可以在调用函数后获取该属性的值:

    export default class SearchModel {   
      constructor() {
         this.start = ''; 
         this.meta = '';
         this.coors = [];
         this.address = 'none';
       }
    
       //translate coordinates to address   
       async getAdress(coordinates) {
         try {
           let geocodeCoordinates = `https://maps.googleapis.com/maps/api/geocode/json?latlng=${this.coors[0]},${this.coors[1]}&key=${process.env.API_GM_KEY}`
           const rawData = await fetch(geocodeCoordinates);
           //console.log(await rawData.json());
           return await rawData.json();
         } catch (error) {
           return new Error(`Wild ERROR occured, can't get LocObj. Details: ${error}`);
         }   }
    
       async displayAdress(coordinates) {
         const data = await this.getAdress(coordinates);
         const dataAdress = await data.results[0].formatted_address;
         this.address = await dataAdress;   }
    
     }
    

    【讨论】:

      猜你喜欢
      • 2020-12-18
      • 2018-02-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-10
      • 2016-03-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多