简体   繁体   中英

response.json is undefined express.js

So here's my code for now:

    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);
  }
}

Everytime I use this, the webdata variable is undefined. Why is this happening? Why isn't returning the data that I requested for?

Thank you.

You are not awaiting for the second promise to be resolved

const webData =  await response.json();

Example:

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM