why post request is not returning the right value

Viewed 21

I am trying to get an object and then post its temp. here i get the object by the getweather function and then i made postweather function to post date (variable i made), content (user input) and temp (temp of the object i got by api).

everything is working great but the temp is retrieved as empty object even though i console.log it and is printed as a number.

async function getWeather(url) {
  fetch(url)
    .then((res) => {
      return res.json();
    })
    .then((data) => {
      // console.log(data);
      //save temp to globall variable
      globalThis.temp = data.main.temp;
    })
    .catch((error) => console.log("error:", error));

  postWeather("/post", temp);
}

//just a fun to check on temp and is printed in numbers

function noha() {
  console.log(temp);
}

//post async
async function postWeather(url) {
  try {
    await fetch(url, {
      method: "POST",
      headers: {
        "Content-Type": "application/json"
      },
      body: JSON.stringify({
        date: newDate,
        temp,
        feelings
      })
    });
  } catch (error) {
    console.log("error post", error);
  }
}
0 Answers
Related