首页 > 解决方案 > 访问从异步函数返回的对象中的属性 - NodeJS

问题描述

我试图从 Google Geocoding API 中找到坐标(纬度和经度),但我不知道如何访问我返回的对象中的属性。这是代码:

const geoCoor = async function () {
  const url = `https://maps.googleapis.com/maps/api/geocode/json?address=${address}&key=${API_KEY}`;
  const response = await request.get(url);
  return response
};

(async function(){
  let data = await geoCoor();
  console.log(data.results[0]);
})();

I've tried wrapping it in an IIFE, but when I try to access the array, I am still getting the "undefined" error message.

标签: javascriptnode.js

解决方案


推荐阅读