I am trying to make my first API call with NodeJS by using node-fetch, however I am getting UNDEFINED as error.
The call works great on my browser link, however with the code below, it returns undefined:
import fetch from 'node-fetch';
const latLongAirports = [{
"name": "Madrid",
"iata": "MAD",
"lat": 40.49565434242003,
"long": -3.574541319609411,
},{
"name": "Los Angeles",
"iata": "LAX",
"lat": 33.93771087455066,
"long": -118.4007447751959,
},{
"name": "Mexico City",
"iata": "MEX",
"lat": 19.437281814699613,
"long": -99.06588831304731,}
]
export function getTemperature(iata){
let data = latLongAirports.find(el => el.iata === iata);
var url = "http://www.7timer.info/bin/api.pl?lon=" + data.long + "&lat=" + data.lat + "&product=astro&output=json"
console.log(url);
async () =>{
const response = fetch(url);
const data2 = await response.json();
console.log(data2);
}
}
console.log(getTemperature('MEX'));
Any ideas why I am not getting the data?
getTemperaturedoesn't even have areturnstatement. Also you have constructed aasync ()=>{}but never invoke it.fetch(url)andawait response.json()don't run at all.console.log is run at the same time with your await response.json()no, there is no multi-threading.so will always be undefinedno, it is undefined because there is noreturn, but you could e.g. return a Promise.