I try to get data from weather API, but something doesnt work.
If I understand the instructions (https://openweathermap.org/current) correctly, I first need the coordinates, which I have saved in variables. However, when I try to find out the weather using the coordinates, I get the following error in my console:
Bad request 400.
and the latitudes “lon” and lat are set to undefined:
https://api.openweathermap.org/data/2.5/weather?lat=undefined&lon=undefined&appid=MyKey
(MyKey is a placeholder because I don’t want anyone to know my real key)
Here is my JS Code:
"use strict";
let API_KEY = "myKey";
let LINK_API = "http://api.openweathermap.org/geo/1.0/direct?q=berlin";
let NEW_API_LINK = "https://api.openweathermap.org/data/2.5/weather";
let lat;
let lon;
let fetchFunc = async function () {
let fetch_begin_for_LINK_API= await fetch(LINK_API+"&appid="+API_KEY);
let data_for_LINK_API = fetch_begin_for_LINK_API.json();
data_for_LINK_API.then(result => {
lat = result[0].lat;
lon = result[0].lon;
});
let fetch_begin_for_NEW_API_LINK = await fetch(NEW_API_LINK+"?lat="+
lat+"&lon="+lon+
"&appid="+API_KEY);
let data_for_NEW_API_LINK = fetch_begin_for_NEW_API_LINK.json();
console.log(data_for_NEW_API_LINK);
}
fetchFunc();