diff --git a/examples/with-mongodb-mongoose/lib/dbConnect.ts b/examples/with-mongodb-mongoose/lib/dbConnect.ts index 2e03265f4d126..516356785c431 100644 --- a/examples/with-mongodb-mongoose/lib/dbConnect.ts +++ b/examples/with-mongodb-mongoose/lib/dbConnect.ts @@ -3,14 +3,6 @@ declare global { var mongoose: any; // This must be a `var` and not a `let / const` } -const MONGODB_URI = process.env.MONGODB_URI!; - -if (!MONGODB_URI) { - throw new Error( - "Please define the MONGODB_URI environment variable inside .env.local", - ); -} - let cached = global.mongoose; if (!cached) { @@ -18,6 +10,14 @@ if (!cached) { } async function dbConnect() { + const MONGODB_URI = process.env.MONGODB_URI!; + + if (!MONGODB_URI) { + throw new Error( + "Please define the MONGODB_URI environment variable inside .env.local", + ); + } + if (cached.conn) { return cached.conn; }