-
Notifications
You must be signed in to change notification settings - Fork 0
/
connect.js
29 lines (24 loc) · 851 Bytes
/
connect.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
const mongoose = require("mongoose");
require("dotenv").config();
const state = mongoose.connection.readyState;
const connecting = mongoose.connection.states.connecting;
const connected = mongoose.connection.states.connected;
if(state !== connecting && state !== connected){
const uri = process.env.MONGODB_URI;
if(!uri)
throw new Error("Environment variable MONGODB_URI is not specified");
// Connect to MongoDB
mongoose.connect(uri, { useNewUrlParser: true })
.catch(err => {
console.log(err);
});
// Check for connection errors and confirm connection
const db = mongoose.connection;
db.on("error", console.error.bind(console, "connection error:"));
db.once("open", () => {
console.log("Connected to CMS database");
});
}else {
console.log(mongoose.connection.readyState);
console.log("Already connected to a MongoDB");
}