//OpenWeather Info
const weatherKey = '*********';
const weatherURL = 'https://api.openweathermap.org/data/2.5/weather'
//Page Elements
const input = document.querySelector("#input").value;
const button = document.querySelector('#submit');
//Fetch
const getWeather = async () => {
try {
const apiURL = weatherURL+ "?&q=" + input + "&APPID=" + weatherKey;
console.log(apiURL);
const response = await fetch(apiURL);
if (response.ok) {
const jsonResponse = await response.json();
console.log(jsonResponse);
return jsonResponse;
} else {
console.log("request failed!");
}
} catch (error) {
console.log(error);
}
**}**
const renderWeather = (data) => {
document.querySelector("#weather-degrees").innerHTML = data.main.temp;
}
const executeSearch = () => {
getWeather().then(data => renderWeather(data));
}
button.addEventListener('click', executeSearch());
Я не могу получить значение ввода при вводе в текстовое поле, и он не получает APIURL. Еще до того, как я что-то ввожу, консоль выдает мне это. Любая помощь? журналы консоли
Вы должны прочитать входное значение прямо перед вызовом функции выборки!
Вы должны быть осторожны при размещении ключей API в Интернете, даже если вы используете бесплатный уровень.