admin管理员组文章数量:1188430
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/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
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
Share Improve this question asked Jan 25 at 0:56 Gerardo AcedoGerardo Acedo 54 bronze badges 1- 3 What do you mean by "it does not work"? What is the error message? – Wernfried Domscheit Commented Jan 25 at 10:12
2 Answers
Reset to default -1In your MongoDB connection string, you have not provided the password:
"mongodb+srv://geraboy:(password)@holamundocert.wfq1b.mongodb.net/miApp?retryWrites=true&w=majority&appName=HolaMundoCert"
You need to replace (password) with your actual MongoDB database access password. Here’s how you can get it:
- Visit MongoDB Cloud
- Navigate to your database called HolaMundoCert.
- Go to the Security section in the left sidebar.
- Click on Database Access.
- Click on Add New Database User.
- Create a new user, assign the role of Atlas Admin (or a role that suits your needs), and set a password.
After creating the user, replace (password) with the password you just set in your connection string. It should look like this:
mongoose.connect("mongodb+srv://geraboy:<your_password>@holamundocert.wfq1b.mongodb.net/miAppretryWrites=true&w=majority&appName=HolaMundoCert);
At this point your problem resolved but if still cause an issue then one last step
- In the left sidebar, click on Network Access under the Security section.
- Under IP Whitelist, click on Add IP Address. click Allow Access from Anywhere or manually enter 0.0.0.0/0 to allow access from all IP addresses.
- Click Confirm.
Note: Allowing access from anywhere (0.0.0.0/0) can be a security risk for production environments, so make sure you restrict IP addresses to trusted ones once your development phase is complete.
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.
本文标签: javascriptCan39t connect to my mongdb database using mongoose (nodejs)Stack Overflow
版权声明:本文标题:javascript - Can't connect to my mongdb database using mongoose (nodejs) - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738376586a2083489.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论