I want an alert to pop-up when the user enters an invalid city name.
Tried using if-else and try-catch method, but not working. I am using the Weather API to get the weather and temperature.
const getWeather = () => {
let cityName = $("#cityName").val();
let apiCall = `http://api.openweathermap.org/data/2.5/weather?q=${cityName}&mode=json&units=metric&appid=${
CONSTANTS.appId
}`;
$.getJSON(apiCall, weatherData => {
let cityName = weatherData.name;
let countryName = weatherData.sys.country;
let description = weatherData.weather[0].description;
let tempMin = weatherData.main.temp_min;
let tempMax = weatherData.main.temp_max;
$("#city").text(cityName);
$("#detail").text(description);
$("#country").text(countryName);
$("#mintemp").html(`Minimum: ${tempMin}<span>℃</span>`);
$("#maxtemp").html(`Maximum: ${tempMax}<span>℃</span>`);
});
};
Whenever an invalid input is entered I get this message in the console.
GET http://api.openweathermap.org/data/2.5/weather?q=sdx&mode=json&units=metric&appid=d568dc579415266146ede4b4f9de029b 404 (Not Found)
jquery-3.4.1.js:9837
I don't want the error to appear, but instead use the error to display a message stating invalid user input.
the entire code link is https://github.com/Shefali-Upadhyay/weather-app