I am making a simple weather application using the Weather API and React. But I am having problems when the user enters a non-existent city because despite trying to deal with it, a "GET 404 (Not Found)" still appears on the console.
Fetch code:
if (evt.key === "Enter") {
fetch(`${api.base}weather?q=${query}&units=metric&APPID=${api.key}`)
.then(res => {
if (res.status === 200) {
res.json().then(result => {
setWeather(result);
setQuery('');
});
} else {
console.log('Erro: ', res);
}
})
.catch(err => { console.log('Failure'); });
}
Input code:
<div className="search-box">
<input
type="text"
className="search-bar"
placeholder="Search..."
onChange={e => setQuery(e.target.value)}
value={query}
onKeyPress={search}
/>
</div>;
Console error:
GET https://api.openweathermap.org/data/2.5/weather?q=kdjklsjdl&units=metric&APPID=f3f25e406a03349031cc25ed16a8196d 404 (Not Found)