Hello everyone, I am struggling to find the reason why I cant connect with my database on MongoDB. This is my first project on java ever and it is going to be an App. I think I have the wrong line of code for the connection I intended to use on MongoDB and I kinda got confused with all the Methods. I am using VS Code and I think using the MongoDB extension on VS is the most practical going forward. So if anyone has some experience working with these tools I would appreciate the help. When trying to run the server this is the error message iI get
Of course I have my real password instead of just "password" there, for safety reasons I share it like this
this is the database.js I have:
const mongoose = require('mongoose');const connectDB = async () => { try { await mongoose.connect(process.env.MONGO_URI); // No need for options now console.log('MongoDB Connected...'); } catch (err) { console.error(err.message); process.exit(1); }};module.exports = connectDB;
const mongoose = require('mongoose');const connectDB = async () => { try { await mongoose.connect(process.env.MONGO_URI); // No need for options now console.log('MongoDB Connected...'); } catch (err) { console.error(err.message); process.exit(1); }};module.exports = connectDB;
`
and this is on my root index.js to connect with mongodb:
// Initialize environment variablesconst dotenv = require('dotenv');dotenv.config(); // Make sure this is called at the very topconsole.log(process.env.MONGO_URI); // Log the MongoDB URIconst express = require('express');const connectDB = require('./database'); // Database connection
// Initialize environment variablesconst dotenv = require('dotenv');dotenv.config(); // Make sure this is called at the very topconsole.log(process.env.MONGO_URI); // Log the MongoDB URIconst express = require('express');const connectDB = require('./database'); // Database connection
Thank you in advance if you need any more information let me know.