Fix: Add type guard for possibly undefined mongoose connection db
This commit is contained in:
1 parent
781826d934
commit
e4a484df45
1 file changed
+10
@@ -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<Agent[]> {
|
||||
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);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user