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); }