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}`); } };