From 90bb40be3d10731f1f60df3f120fc5f43be77f67 Mon Sep 17 00:00:00 2001 From: Nidhibansalll Date: Sun, 27 Oct 2024 20:16:41 +0530 Subject: [PATCH] Update db.js // i just made some changes so that it can add connection options to improve performance and handle potential issues and want to log more details about the error, such as the error code so that it ,may create more easiness and reusble of code. Signed-off-by: Nidhibansalll --- config/db.js | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/config/db.js b/config/db.js index 113f1879..6ca12619 100644 --- a/config/db.js +++ b/config/db.js @@ -1,14 +1,27 @@ + +// i just made some changes so that it can add connection options to improve performance and handle potential issues and want to log more details about the error, such as the error code so that it ,may create more easiness and reusble of code. + + const mongoose = require("mongoose"); mongoose.set("strictQuery", true); const connectDb = async () => { const uri = process.env.MONGODB_URI; + + if (!uri) { + console.error("Please set the MONGODB_URI environment variable."); + process.exit(1); + } + try { - const connect = await mongoose.connect(uri, {}); + const connect = await mongoose.connect(uri, { + useNewUrlParser: true, + useUnifiedTopology: true, + }); console.log(`Connected to Db: ${connect.connection.host}`); } catch (error) { - console.log(`MongoDb connection Error:${error}`); + console.error(`MongoDB connection error: ${error.message}`); } };