-
Notifications
You must be signed in to change notification settings - Fork 5
/
db.js
27 lines (23 loc) · 1.09 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
const mongoose = require('mongoose');
require('dotenv').config();
console.log('Attempting to load environment variables...'); // gpt_pilot_debugging_log
if (!process.env.MONGODB_URI) {
console.error('Missing MONGODB_URI in environment variables.'); // gpt_pilot_debugging_log
}
const connectionStr = process.env.MONGODB_URI || 'mongodb://localhost:27017/complex_chat_app_db';
try {
mongoose.connect(connectionStr)
.then(() => console.log(`Mongoose connected to: ${connectionStr}`))
.catch(err => console.error(`Error connecting to MongoDB: ${err.message}`, err.stack)); // gpt_pilot_debugging_log
} catch (err) {
console.error(`Unexpected error initializing MongoDB connection: ${err.message}`, err.stack); // gpt_pilot_debugging_log
}
mongoose.connection.on('connected', () => {
console.log(`Mongoose connected to: ${connectionStr}`);
});
mongoose.connection.on('error', (err) => {
console.error(`Mongoose connection error: ${err.message}`, err.stack); // gpt_pilot_debugging_log
});
mongoose.connection.on('disconnected', () => {
console.log('Mongoose disconnected');
});