-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.js
43 lines (35 loc) · 1.13 KB
/
db.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// const mongoose = require('mongoose');
// //mongodb uri
// const MONGO_URI = process.env.MONGO_URI;
// //connect to db
// const connectDb = async () => {
// try {
// await mongoose.connect(MONGO_URI, {
// useNewUrlParser: true,
// useUnifiedTopology: true,
// });
// console.log('Mongoose connected to MONGODB Atlas');
// } catch (err) {
// console.log(err.message);
// process.exit(1);
// }
// };
// module.exports = connectDb;require('dotenv').config(); // Ensure dotenv is configured
const mongoose = require('mongoose');
//mongodb uri
const MONGO_URI = process.env.MONGO_URI;
const connectDb = async () => {
// Check if MONGO_URI is defined
if (!MONGO_URI) {
console.error('MongoDB URI is not defined. Please add MONGO_URI in your .env file.');
process.exit(1); // Exit the process with failure code
}
try {
await mongoose.connect(MONGO_URI);
console.log('Mongoose connected to MongoDB Atlas');
} catch (err) {
console.error(`Failed to connect to MongoDB: ${err.message}`);
process.exit(1); // Exit the process if there's an error
}
};
module.exports = connectDb;