handle unknown cities

Zoe Kim 7 months ago
parent b1ae2b16bf commit f1a2223d99969d05336c53940b1e87a1fda19740
1 changed files with 1 additions and 0 deletions
+1-0weather.js
View File
@@ -4,6 +4,7 @@
44 form.addEventListener("submit", async e => {
55 e.preventDefault();
66 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; }
78 const place = geo.results[0];
89 const w = await (await fetch(`https://api.open-meteo.com/v1/forecast?latitude=${place.latitude}&longitude=${place.longitude}&current=temperature_2m`)).json();
910 out.textContent = `${place.name}: ${w.current.temperature_2m} °C`;