簡體   English   中英

response.json 未定義 express.js

[英]response.json is undefined express.js

所以這是我現在的代碼:

    function geoPerformAction(e) {
  getGeoApiData(document.getElementById('zipPostCode').value)
    .then((APIarr) => {
      postGeoData('/geoadd', { Lat: APIarr[0] });
    })
    .then(function () {
      updateUIGeo();
    })
}

//Friend helped with me with get API data
/* Function to GET Web API Data*/
const getGeoApiData = async ( place) => {
  const response = await fetch("https://pokeapi.co/api/v2/pokemon/" + place + "/");
  try {
    const webData =  response.json();

    const Pla = webData;
    console.log(Pla);


const APIarr = [Pla];

    return APIarr;
  }
  catch (error) {
    console.log("error", error);
  }
}

每次我使用它時,webdata 變量都是未定義的。 為什么會這樣? 為什么不返回我請求的數據?

謝謝你。

您不是在等待解決第二個 promise

const webData =  await response.json();

例子:

async function fetchAsync () {
  // await response of fetch call
  const response = await fetch('https://api.github.com');
  // only proceed once promise is resolved
  const data = await response.json();
  // only proceed once second promise is resolved
  return data;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM