-1

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

1
  • 3
    What do you mean by "it does not work"? What is the error message? Commented Jan 25 at 10:12

1 Answer 1

-2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Can you edit this answer to be more specific? Remove the various things that didn't help, but describe what exactly you needed to update Node to, and why there was an incompatibility there.
According to stackoverflow.com/a/79128693/3027266 the problems seem to be the mongoose version rather than node.
For me that's what worked. I was using node v18 after i installed version 22.13.1 it started working. mongoose version was up to date. I tried to access the database from another computer and it worked, before updating node in my computer. @DavidMaze

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.