Search code examples
javascriptnode.jsmongodbmongoosedatabase-connection

Can't connect to my mongdb database using mongoose (nodejs)


Im trying to connect to my database. Its working if i use mongodb compass but then im using mongoose to connect it doesn't work.

I keep getting this error:

Mongoose connection error MongoServerSelectionError: Client network socket disconnected before secure TLS connection was established
    at Topology.selectServer (C:\Users\gerar\Desktop\HtmlPractica-HolaMundo\htmlPractica\APIrest\node_modules\mongodb\lib\sdam\topology.js:320:38)
    at async Topology._connect (C:\Users\gerar\Desktop\HtmlPractica-HolaMundo\htmlPractica\APIrest\node_modules\mongodb\lib\sdam\topology.js:204:28)
    at async Topology.connect (C:\Users\gerar\Desktop\HtmlPractica-HolaMundo\htmlPractica\APIrest\node_modules\mongodb\lib\sdam\topology.js:156:13)
    at async topologyConnect (C:\Users\gerar\Desktop\HtmlPractica-HolaMundo\htmlPractica\APIrest\node_modules\mongodb\lib\mongo_client.js:233:17)
    at async MongoClient._connect (C:\Users\gerar\Desktop\HtmlPractica-HolaMundo\htmlPractica\APIrest\node_modules\mongodb\lib\mongo_client.js:246:13)
    at async MongoClient.connect (C:\Users\gerar\Desktop\HtmlPractica-HolaMundo\htmlPractica\APIrest\node_modules\mongodb\lib\mongo_client.js:171:13)
    at async NativeConnection.createClient (C:\Users\gerar\Desktop\HtmlPractica-HolaMundo\htmlPractica\APIrest\node_modules\mongoose\lib\drivers\node-mongodb-native\con
nection.js:320:3

This is my api.js file

const express = require("express");
const mongoose = require("mongoose");
const user = require("./user.controller");
const port = 3000;
const app = express();

app.use(express.json());

mongoose.connect(
  "mongodb+srv://geraboy:(password)@holamundocert.wfq1b.mongodb.net/miApp?retryWrites=true&w=majority&appName=HolaMundoCert"
);

mongoose.connection.on("connected", () => {
  console.log("Mongoose connected to MongoDB");

  app.listen(port, () =>
    console.log(`Starting server on port ${port}`)
  );
});

mongoose.connection.on("error", (error) => {
  console.log("Mongoose connection error", error);
});

app.get("/", user.list);
app.post("/", user.create);
app.get("/:id", user.get);
app.put("/:id", user.update);
app.patch("/:id", user.update);
app.delete("/:id", user.destroy);

This is my package.json

{
  "name": "apirest",
  "version": "1.0.0",
  "description": "",
  "main": "api.js",
  "scripts": {
    "start": "nodemon api.js"
  },
  "keywords": [],
  "author": "Gerardo Acedo",
  "license": "ISC",
  "dependencies": {
    "express": "^4.21.2",
    "mongodb": "^6.12.0",
    "mongoose": "^8.9.5"
  },
  "devDependencies": {
    "nodemon": "^3.1.9"
  }
}

I tried setting the IP network access to 0.0.0.0/0 on the mongodb cluster's website, also tried adding my own IP direction. Already created inbound and outbound permissions for port 27017.

Also tried setting my DNS to 8.8.8.8


Solution

  • I ended up finding the solution to my problem. If someone else is having the same issue with the "IP not being whitelisted" even though it is, and you've tried the following:

    • Changing the DNS to 8.8.8.8
    • Turning off your VPN
    • Creating new outbound and inbound rules for port 27017

    ...and still can't find the solution, check your Node.js version and make sure it is updated. That was the mistake I made. I checked the versions of Mongoose, Express, and MongoDB, and everything looked good.

    It turned out to be the Node.js update.