From 9bddf8b76cf702ce572aa2d4069879bf67b10bec Mon Sep 17 00:00:00 2001 From: Rachit Srivastava <64166730+Ethics03@users.noreply.github.com> Date: Sun, 1 Dec 2024 13:03:01 +0530 Subject: [PATCH] fix: Fixed undefined temp and resolved response handling added .catch for errors --- assets/js/weather.js | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/assets/js/weather.js b/assets/js/weather.js index 3d0dbb999..4b00c6b7f 100644 --- a/assets/js/weather.js +++ b/assets/js/weather.js @@ -39,20 +39,31 @@ function setPosition(position) { function getWeather(latitude, longitude) { let api = `https://api.openweathermap.org/data/2.5/weather?lat=${latitude}&lon=${longitude}&lang=${CONFIG.language}&appid=${key}`; + fetch(api) - .then(function(response) { - let data = response.json(); - return data; + .then(response => { + if (!response.ok) { + throw new Error(`API Error: ${response.status} - ${response.statusText}`); + } + return response.json(); }) - .then(function(data) { - let celsius = Math.floor(data.main.temp - KELVIN); - weather.temperature.value = tempUnit == 'C' ? celsius : (celsius * 9) / 5 + 32; - weather.description = data.weather[0].description; - weather.iconId = data.weather[0].icon; + .then(data => { + // Validate the API response + if (!data.main || !data.weather || !data.weather[0]) { + + return; + } + + const celsius = Math.floor(data.main.temp - KELVIN); + weather.temperature.value = tempUnit === 'C' + ? celsius + : Math.floor(celsius * 9 / 5 + 32); + weather.description = data.weather[0]?.description || 'Unknown'; + weather.iconId = data.weather[0]?.icon || 'default'; }) - .then(function() { - displayWeather(); - }); + .then(displayWeather) + .catch(error => console.error('Error fetching weather data:', error.message || error)); +} } function displayWeather() {