This repository was archived by the owner on Feb 25, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-mongodb.js
More file actions
62 lines (51 loc) · 2.51 KB
/
test-mongodb.js
File metadata and controls
62 lines (51 loc) · 2.51 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
const { MongoClient } = require('mongodb');
require('dotenv').config();
const uri = process.env.MONGODB_URI || 'mongodb://localhost:27017/room-redesign';
async function testConnection() {
const client = new MongoClient(uri);
console.log(`Testing MongoDB connection to: ${uri.split('@').length > 1 ? uri.split('@')[1] : uri}`);
try {
// Connect to MongoDB
await client.connect();
console.log('✅ Successfully connected to MongoDB!');
// List all databases (this will verify we have proper permissions)
const dbs = await client.db().admin().listDatabases();
console.log('\nAvailable databases:');
dbs.databases.forEach(db => console.log(` - ${db.name}`));
// Check if using Atlas or local
const isAtlas = uri.includes('mongodb+srv');
if (isAtlas) {
console.log('\nYou are using MongoDB Atlas. Ensure your IP address is whitelisted in the Atlas dashboard.');
} else {
console.log('\nYou are using a local MongoDB instance.');
}
} catch (error) {
console.error('❌ Error connecting to MongoDB:', error);
if (uri.includes('mongodb+srv')) {
console.log('\nSince you are using MongoDB Atlas, this error might be due to:');
console.log('1. Your IP address is not whitelisted in Atlas');
console.log('2. Incorrect username or password');
console.log('3. Atlas cluster is not running or is being maintained');
console.log('\nTo fix IP whitelisting:');
console.log('- Log in to MongoDB Atlas: https://cloud.mongodb.com/');
console.log('- Select your cluster (Cluster0)');
console.log('- Click on "Network Access" in the left sidebar');
console.log('- Click "Add IP Address" button and add your current IP address');
console.log('\nAlternatively, you can use a local MongoDB instance by changing your .env file:');
console.log('MONGODB_URI=mongodb://localhost:27017/room-redesign');
} else {
console.log('\nSince you are using a local MongoDB instance, this error might be due to:');
console.log('1. MongoDB service is not running');
console.log('2. MongoDB is running on a different port');
console.log('\nTo start MongoDB:');
console.log('- macOS: brew services start mongodb-community');
console.log('- Windows: Ensure the MongoDB service is running');
console.log('- Linux: sudo systemctl start mongod');
}
} finally {
// Close the connection
await client.close();
console.log('\nConnection closed.');
}
}
testConnection();