Connecting to my MongoDB Database

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 querySrv ENOTFOUND _mongodb._tcp.cluster0.mongodb.net and this is the line on my .env i am using to connect to MongoDB MONGO_URI=mongodb+srv://mmetinbirguel:[email protected]/Tierlist?retryWrites=true&w=majority 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 variables
const dotenv = require('dotenv');
dotenv.config(); // Make sure this is called at the very top
console.log(process.env.MONGO_URI); // Log the MongoDB URI


const express = require('express');
const connectDB = require('./database'); // Database connection
// Initialize environment variables
const dotenv = require('dotenv');
dotenv.config(); // Make sure this is called at the very top
console.log(process.env.MONGO_URI); // Log the MongoDB URI


const express = require('express');
const connectDB = require('./database'); // Database connection
Thank you in advance if you need any more information let me know.
6 Replies
JavaBot
JavaBot•4mo ago
⌛ This post has been reserved for your question.
Hey @Metz! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically marked as dormant after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
sooluckyseven
sooluckyseven•4mo ago
do you use maven or gradle? this look like javascript?
Metz
MetzOP•4mo ago
Oh yes this is Javascript hahah oops wrong server then :kekw:
sooluckyseven
sooluckyseven•4mo ago
well, that must be why it dont work well, is your mongodb on your computer? at 127.0.0.1? cluster0.mongodb.net?
Metz
MetzOP•4mo ago
I am not really sure, i was looking where to check but it was connected to database somewhere else in Frankfurt If thats what you mean
JavaBot
JavaBot•4mo ago
Post Closed
This post has been closed by <@743072402702860358> for the following reason:
User left the server

Did you find this page helpful?