-
Notifications
You must be signed in to change notification settings - Fork 0
/
connection.js
35 lines (29 loc) · 1.07 KB
/
connection.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
// Do not change this file
require('dotenv').config();
// const { MongoClient } = require('mongodb');
var mongoose = require('mongoose');
async function main(callback) {
// const URI = process.env.MONGO_DB_URI; // Declare MONGO_URI in your .env file
// const client = new MongoClient(URI, { useNewUrlParser: true, useUnifiedTopology: true });
// try {
// // Connect to the MongoDB cluster
// await client.connect();
// // Make the appropriate DB calls
// await callback(client);
// } catch (e) {
// // Catch any errors
// console.error(e);
// throw new Error('Unable to Connect to Database')
// }
const mongoDB = process.env.MONGO_DB_URI;
try{
mongoose.connect(mongoDB, { useNewUrlParser: true , useUnifiedTopology: true});
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'MongoDB connection error:'));
await callback(db)
} catch(e){
console.error(e);
throw new Error('Unable to connect to DB')
}
}
module.exports = main;