handle unknown cities
1 changed files with 1 additions and 0 deletions
weather.js
View File
| @@ -4,6 +4,7 @@ | ||
| 4 | 4 | form.addEventListener("submit", async e => { |
| 5 | 5 | e.preventDefault(); |
| 6 | 6 | const geo = await (await fetch(`https://geocoding-api.open-meteo.com/v1/search?name=${form.city.value}&count=1`)).json(); |
| 7 | + if (!geo.results) { out.textContent = "City not found"; return; } | |
| 7 | 8 | const place = geo.results[0]; |
| 8 | 9 | const w = await (await fetch(`https://api.open-meteo.com/v1/forecast?latitude=${place.latitude}&longitude=${place.longitude}¤t=temperature_2m`)).json(); |
| 9 | 10 | out.textContent = `${place.name}: ${w.current.temperature_2m} °C`; |