-1
app.get("/", function (req, res) {
  async function showData() {
    const res = await fetch("api.openweathermap.org/data/2.5/weather?q=Berlin&appid={apiID}");
    const data = await res.json();
    console.log(data);
  }
})

//Here when I run node app.js this should log the data inside terminal but I am getting error.

Derek Wang
  • 10,098
  • 4
  • 18
  • 39
Suraj Arya
  • 27
  • 3
  • Why are you using await before res.json()? could you please explain? – Vipul Patil Oct 12 '20 at 08:00
  • here await before res.json() is similar to .then. Basically I am using await before res.json() to get the response in JSON format after await the API url link. – Suraj Arya Oct 12 '20 at 08:27

1 Answers1

0

Fetch returns a response stream. Checkout the answer to this question. Use request-promise/request module or handle the stream returned by fetch.

qamar
  • 63
  • 5
  • In link I saw we are require("request") module to fetch API call. Since in plane JS we don't need to require any module we directly use fetch function and .then() is used to get the date. And in the asked question I am trying follow the same procedure without using request module. Thanks! – Suraj Arya Oct 12 '20 at 08:29
  • Ok. Try using stream to get data and then in res.on('end',callback) process it, or log it. And I could be inferring this perhaps from the incomplete code, but are you calling showData() somewhere? – qamar Oct 12 '20 at 09:14