diff --git a/src/app/api/getState/route.ts b/src/app/api/getState/route.ts index af70a41..744c024 100644 --- a/src/app/api/getState/route.ts +++ b/src/app/api/getState/route.ts @@ -9,7 +9,8 @@ dotenv.config({ path: `.env.local` }); export async function POST(req: Request) { const stateManager = await StateManager.getInstance(); - const state = (await stateManager.getLatestStatus()).status; + const latestStatus = await stateManager.getLatestStatus(); + const state = latestStatus ? latestStatus.status : []; return NextResponse.json(state); } diff --git a/src/app/utils/state.ts b/src/app/utils/state.ts index c3f7ac9..02aa330 100644 --- a/src/app/utils/state.ts +++ b/src/app/utils/state.ts @@ -144,7 +144,6 @@ class StateManager { if (error) { console.error("error: ", error); } - return data![0]; } diff --git a/src/components/Tamagotchi.tsx b/src/components/Tamagotchi.tsx index 79ad427..c6b60f8 100644 --- a/src/components/Tamagotchi.tsx +++ b/src/components/Tamagotchi.tsx @@ -54,14 +54,17 @@ export function Tamagotchi() { const fetchData = async () => { try { const response = await fetch("/api/getState", { method: "POST" }); + if (response.ok) { const jsonData = await response.json(); - if (!!!jsonData.comment) { - jsonData.comment = "No comments"; + if (!!jsonData) { + if (!!!jsonData.comment) { + jsonData.comment = "No comments"; + } + setTamagotchiState(jsonData); + console.log(jsonData); + handleDeath(jsonData, pollInterval); } - setTamagotchiState(jsonData); - console.log(jsonData); - handleDeath(jsonData, pollInterval); } } catch (error) { console.error("Error fetching data:", error);