Skip to content
Snippets Groups Projects

Resolve "Configure MongoDB Connection"

1 file
+ 1
11
Compare changes
  • Side-by-side
  • Inline
+ 1
11
@@ -6,19 +6,9 @@ async function connectDB() {
@@ -6,19 +6,9 @@ async function connectDB() {
// Connect using the MongoDB URI from environment variables
// Connect using the MongoDB URI from environment variables
const conn = await mongoose.connect(process.env.MONGO_URI || "mongodb://127.0.0.1:27017/todo_app");
const conn = await mongoose.connect(process.env.MONGO_URI || "mongodb://127.0.0.1:27017/todo_app");
console.log(`MongoDB connected: ${conn.connection.host}`);
console.log(`MongoDB connected: ${conn.connection.host}`);
// Get the database object from the connection
const db = conn.connection.db;
// Check if the collections exist and create them if not
await db.createCollection('users');
await db.createCollection('todos');
await db.createCollection('global');
console.log('Collections created successfully!');
} catch (err) {
} catch (err) {
console.error(`Error: ${err.message}`);
console.error(`Error: ${err.message}`);
process.exit(1); // Exit the process with failure if the connection or collection creation fails
process.exit(1); // Exit the process with failure if the connection fails
}
}
}
}
Loading