Next.js Discord

Discord Forum

In the Product Environment, Not loading data from MongoDB !!

Unanswered
ali argndgn posted this in #help-forum
Open in Discord
Avatar
ali argndgnOP
import mongoose from "mongoose";

const URI = process.env.MONGO_URI;

if (!URI) {
throw new Error(
"Please define the MONGODB_URI environment variable inside .env.local"
);
}
let cached = global.mongoose;

if (!cached) {
cached = global.mongoose = { conn: null, promise: null };
}

async function dbConnect() {
if (cached.conn) {
console.log("cached db");
return cached.conn;
}

if (!cached.promise) {
const options = {
useNewUrlParser: true,
useUnifiedTopology: true,
ssl: true,
tlsAllowInvalidCertificates: true,
dbName: "HrDB",
};
cached.promise = mongoose.connect(URI, options).then((mongoose) => {
console.log("new db");
return mongoose;
});
}

try {
cached.conn = await cached.promise;
} catch (e) {
cached.promise = null;
throw e;
}

return cached.conn;
}

export default dbConnect;

No problem in local environment
When I put it in the product environment, it works normally for a while. Then it doesn't load the data from mongoDB
**After a while, the database stops responding. And timeout error returns.

4 Replies

Avatar
tafutada777
Where do you host?
Avatar
ali argndgnOP
Azure Devops
Avatar
tafutada777
isn't it a build pipeline to build a Docker image? where do you deploy the image? Container Apps?
Avatar
ali argndgnOP
Docker