Skip to content

Commit 0699cb3

Browse files
committed
Added dotenv dependency to package.json and updated package-lock.json
1 parent 0e8d057 commit 0699cb3

File tree

5 files changed

+56
-0
lines changed

5 files changed

+56
-0
lines changed

.env

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
MONGODB_URI=mongodb+srv://group-b:[email protected]/mydatabase?retryWrites=true&w=majority

app/testConnection.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
require('dotenv').config(); // Load environment variables from .env
2+
import { MongoClient } from 'mongodb';
3+
4+
async function testConnection() {
5+
const uri = process.env.MONGODB_URI; // Get connection string from .env
6+
const client = new MongoClient(uri, { useNewUrlParser: true, useUnifiedTopology: true });
7+
8+
try {
9+
// Try connecting to the database
10+
await client.connect();
11+
console.log('Connected to MongoDB successfully');
12+
} catch (error) {
13+
// Log any connection errors
14+
console.error('Failed to connect to MongoDB:', error);
15+
} finally {
16+
// Close the connection
17+
await client.close();
18+
}
19+
}
20+
21+
testConnection();

package-lock.json

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"lint": "next lint"
1010
},
1111
"dependencies": {
12+
"dotenv": "^16.4.5",
1213
"keyv": "^5.1.2",
1314
"mongodb": "^6.10.0",
1415
"next": "^15.0.0",

testConnection.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
require('dotenv').config(); // Load environment variables from .env
2+
const { MongoClient } = require('mongodb'); // Use require instead of import
3+
4+
async function testConnection() {
5+
const uri = process.env.MONGODB_URI; // Get connection string from .env
6+
const client = new MongoClient(uri, { useNewUrlParser: true, useUnifiedTopology: true });
7+
8+
try {
9+
// Try connecting to the database
10+
await client.connect();
11+
console.log('Connected to MongoDB successfully');
12+
} catch (error) {
13+
// Log any connection errors
14+
console.error('Failed to connect to MongoDB:', error);
15+
} finally {
16+
// Close the connection
17+
await client.close();
18+
}
19+
}
20+
21+
testConnection();

0 commit comments

Comments
 (0)