Skip to content

Commit 4bfcb24

Browse files
committed
Fix MongoDB connection and remove deprecated options - Remove deprecated useNewUrlParser and useUnifiedTopology options - Add error handling for missing MONGODB_URI environment variable - Only load dotenv in non-production environments
1 parent d5364f4 commit 4bfcb24

2 files changed

Lines changed: 14 additions & 7 deletions

File tree

src/server/db.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,16 @@ const mongoose = require('mongoose');
22

33
const connect = async () => {
44
try {
5-
const mongoURI = process.env.MONGODB_URI || 'mongodb://localhost:27017/abugida';
5+
const mongoURI = process.env.MONGODB_URI;
66

7-
await mongoose.connect(mongoURI, {
8-
useNewUrlParser: true,
9-
useUnifiedTopology: true,
10-
});
7+
if (!mongoURI) {
8+
console.error('MONGODB_URI environment variable is not set!');
9+
console.error('Please set MONGODB_URI in Railway environment variables.');
10+
process.exit(1);
11+
}
12+
13+
// Remove deprecated options - they're not needed in Mongoose 6+
14+
await mongoose.connect(mongoURI);
1115

1216
console.log('MongoDB connected successfully');
1317
} catch (error) {

src/server/server.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@ const cors = require('cors');
33
const mongoose = require('mongoose');
44
const path = require('path');
55

6-
// Load environment variables from project root
7-
require('dotenv').config({ path: path.join(__dirname, '../../.env') });
6+
// Load environment variables from project root (for local development only)
7+
// In Railway, environment variables are set directly, so this is optional
8+
if (process.env.NODE_ENV !== 'production') {
9+
require('dotenv').config({ path: path.join(__dirname, '../../.env') });
10+
}
811

912
const authRoutes = require('./routes/auth');
1013

0 commit comments

Comments
 (0)