Fix 500 Internal Server Error: Add error handling for D1 database access in Cloudflare Pages

This commit is contained in:
katelya
2025-09-05 16:05:52 +08:00
parent 142c780b50
commit 07cdaafcb2
3 changed files with 47 additions and 15 deletions
+16 -1
View File
@@ -39,7 +39,22 @@ interface D1ExecResult {
// 获取全局D1数据库实例
function getD1Database(): D1Database {
return (process.env as any).DB as D1Database;
// 在 Cloudflare Pages/Workers 环境中,DB 是全局变量
if (typeof globalThis !== 'undefined' && (globalThis as any).DB) {
return (globalThis as any).DB as D1Database;
}
// 回退到 process.env(用于本地开发)
if ((process.env as any).DB) {
return (process.env as any).DB as D1Database;
}
// 最后尝试从 globalThis.process.env
if (typeof globalThis !== 'undefined' && (globalThis as any).process?.env?.DB) {
return (globalThis as any).process.env.DB as D1Database;
}
throw new Error('D1 database not available. Make sure DB binding is configured.');
}
export class D1Storage implements IStorage {