From 584176baff6dd92a0b0654afa8d14111adf121da Mon Sep 17 00:00:00 2001 From: ChrQR Date: Sun, 12 May 2024 21:58:10 +0200 Subject: [PATCH] error handling on wrong input in search bar --- app/actions.ts | 3 +++ components/LocationSearch.tsx | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/app/actions.ts b/app/actions.ts index 3d73839..b2903ae 100644 --- a/app/actions.ts +++ b/app/actions.ts @@ -10,6 +10,9 @@ export async function getLocation(searchLocation: string): Promise{ throw new Error(`There was an error fetching the data`); } const data: LocationType = await res.json(); + if (!data.results[0].formatted_address){ + throw new Error(`Unable to find the address`); + } return { name: data.results[0].formatted_address, geo: data.results[0].geometry.location diff --git a/components/LocationSearch.tsx b/components/LocationSearch.tsx index 8f3bc96..048aa31 100644 --- a/components/LocationSearch.tsx +++ b/components/LocationSearch.tsx @@ -5,7 +5,7 @@ import { LocationContext } from "@/context/LocationContext"; export default function LocationSearch() { const [searchLocation, setSearchLocation] = useState(""); - const { geoLocation, setGeoLocation } = useContext(LocationContext); + const { setGeoLocation } = useContext(LocationContext); const [pending, setPending] = useState(false); const handleChange = (e: React.ChangeEvent) => {