From e4a484df45ce438aa06440337d80c83ed1ba5aea Mon Sep 17 00:00:00 2001 From: ypratama Date: Wed, 2 Sep 2026 17:24:08 +0700 Subject: [PATCH] Fix: Add type guard for possibly undefined mongoose connection db --- src/lib/actions/agents.ts | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/lib/actions/agents.ts b/src/lib/actions/agents.ts index 56fb2b2..e07035e 100644 --- a/src/lib/actions/agents.ts +++ b/src/lib/actions/agents.ts @@ -20,6 +20,9 @@ export async function updateAgent(agentId: string, label: string) { await mongoose.connect(MONGODB_URI, { serverSelectionTimeoutMS: 5000 }); } const db = mongoose.connection.db; + if (!db) { + throw new Error("Database tidak tersedia."); + } // Check if agent exists in agent_registry const agent = await db.collection('agent_registry').findOne({ $or: [{ uuid: agentId }, { serial: agentId }] }); @@ -54,6 +57,9 @@ export async function deleteAgent(agentId: string) { await mongoose.connect(MONGODB_URI, { serverSelectionTimeoutMS: 5000 }); } const db = mongoose.connection.db; + if (!db) { + throw new Error("Database tidak tersedia."); + } // Check if agent exists in agent_registry const agent = await db.collection('agent_registry').findOne({ $or: [{ uuid: agentId }, { serial: agentId }] }); @@ -104,6 +110,10 @@ export async function getAgents(clientSiteUuid?: string): Promise { return []; // Do not throw to avoid Next.js Server Component crash } const db = mongoose.connection.db; + if (!db) { + console.error("[getAgents Action] Database is undefined."); + return []; + } return await fetchAgentsFromMongo(db, user, SITE_UUID); }