admin管理员组文章数量:1203210
I am following these tutorials as I wanted to start with MongoDB and the MERN stack:
- /@beaucarnes/learn-the-mern-stack-by-building-an-exercise-tracker-mern-tutorial-59c13c1237a1
Everything was fine until my connection with the database failed. I checked the username/password million times, I copied and pasted the original in case I had some syntax error. This is the error:
(node:10156) UnhandledPromiseRejectionWarning: MongooseServerSelectionError: connection <monitor> to 34.249.129.6:27017 closed
at new MongooseServerSelectionError (C:\Users\Mario\Desktop\ReactProject\MongoDB\mern-tracker\node_modules\mongoose\lib\error\serverSelection.js:22:11)
at NativeConnection.Connection.openUri (C:\Users\Mario\Desktop\ReactProject\MongoDB\mern-tracker\node_modules\mongoose\lib\connection.js:808:32)
at Mongoose.connect (C:\Users\Mario\Desktop\ReactProject\MongoDB\mern-tracker\node_modules\mongoose\lib\index.js:333:15)
at Object.<anonymous> (C:\Users\Mario\Desktop\ReactProject\MongoDB\mern-tracker\server\server.js:51:10)
at Module._compile (internal/modules/cjs/loader.js:778:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:831:12)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)
(node:10156) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:10156) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
and this is my server.js:
const express = require("express");
const mongoose = require("mongoose");
require("dotenv").config();
const app = express();
const port = process.env.PORT || 5000;
app.use(cors());
app.use(express.json());
const uri = process.env.ATLAS_URI;
mongoose.connect(uri, {
useNewUrlParser: true,
useCreateIndex: true,
useUnifiedTopology: true
});
const connection = mongoose.connection;
// connection.on("error", error => console.error(error));
connection.once("open", uri => {
console.log("MongoDB database connections established");
});
const exercisesRoute = require("./routes/exercises");
const usersRoute = require("./routes/users");
app.use("./exercises", exercisesRoute);
app.use("./users", usersRoute);
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});
Since the error is saying about Unhandled promise, what I also tried in my mongoose connection is:
mongoose
.connect(uri, {
useNewUrlParser: true,
useCreateIndex: true,
useUnifiedTopology: true
})
.catch(err => {
console.log(Error, err.message);
})
.then(() => console.log("DB Connected!"))
and that gives me the next error:
{ [Function: Error] stackTraceLimit: 16, prepareStackTrace: undefined } 'connection <monitor> to 52.212.0.151:27017 closed'
DB Connected!
and yet, is not connected.
Sometimes in the error, the connection <monitor> to 52.212.0.151:27017 closed'
changes to 34.249.129.6:27017
Another thing that I tried was deleting the Cluster and creating a new one but the same thing happens I am using windows and node v10.16.3
Hope you guys can help me or advice me.
I am following these tutorials as I wanted to start with MongoDB and the MERN stack:
- https://medium.com/@beaucarnes/learn-the-mern-stack-by-building-an-exercise-tracker-mern-tutorial-59c13c1237a1
- https://www.youtube.com/watch?v=7CqJlxBYj-M
Everything was fine until my connection with the database failed. I checked the username/password million times, I copied and pasted the original in case I had some syntax error. This is the error:
(node:10156) UnhandledPromiseRejectionWarning: MongooseServerSelectionError: connection <monitor> to 34.249.129.6:27017 closed
at new MongooseServerSelectionError (C:\Users\Mario\Desktop\ReactProject\MongoDB\mern-tracker\node_modules\mongoose\lib\error\serverSelection.js:22:11)
at NativeConnection.Connection.openUri (C:\Users\Mario\Desktop\ReactProject\MongoDB\mern-tracker\node_modules\mongoose\lib\connection.js:808:32)
at Mongoose.connect (C:\Users\Mario\Desktop\ReactProject\MongoDB\mern-tracker\node_modules\mongoose\lib\index.js:333:15)
at Object.<anonymous> (C:\Users\Mario\Desktop\ReactProject\MongoDB\mern-tracker\server\server.js:51:10)
at Module._compile (internal/modules/cjs/loader.js:778:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:831:12)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)
(node:10156) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:10156) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
and this is my server.js:
const express = require("express");
const mongoose = require("mongoose");
require("dotenv").config();
const app = express();
const port = process.env.PORT || 5000;
app.use(cors());
app.use(express.json());
const uri = process.env.ATLAS_URI;
mongoose.connect(uri, {
useNewUrlParser: true,
useCreateIndex: true,
useUnifiedTopology: true
});
const connection = mongoose.connection;
// connection.on("error", error => console.error(error));
connection.once("open", uri => {
console.log("MongoDB database connections established");
});
const exercisesRoute = require("./routes/exercises");
const usersRoute = require("./routes/users");
app.use("./exercises", exercisesRoute);
app.use("./users", usersRoute);
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});
Since the error is saying about Unhandled promise, what I also tried in my mongoose connection is:
mongoose
.connect(uri, {
useNewUrlParser: true,
useCreateIndex: true,
useUnifiedTopology: true
})
.catch(err => {
console.log(Error, err.message);
})
.then(() => console.log("DB Connected!"))
and that gives me the next error:
{ [Function: Error] stackTraceLimit: 16, prepareStackTrace: undefined } 'connection <monitor> to 52.212.0.151:27017 closed'
DB Connected!
and yet, is not connected.
Sometimes in the error, the connection <monitor> to 52.212.0.151:27017 closed'
changes to 34.249.129.6:27017
Another thing that I tried was deleting the Cluster and creating a new one but the same thing happens I am using windows and node v10.16.3
Hope you guys can help me or advice me.
Share Improve this question asked Mar 6, 2020 at 12:22 Mario GarciaMario Garcia 3072 gold badges5 silver badges17 bronze badges3 Answers
Reset to default 19Try this It seems like you are using MongoDB atlas
mongoose
.connect( uri, { useNewUrlParser: true, useCreateIndex: true, useUnifiedTopology: true })
.then(() => console.log( 'Database Connected' ))
.catch(err => console.log( err ));
and go to your MongoDB Atlas
-> NetworkAccess
-> Edit
-> and add Current IP address
it works!!
Try this
mongoose.connect(uri, {
useNewUrlParser: true,
useCreateIndex: true,
useUnifiedTopology: true
}).then(res=>{
console.log("DB Connected!")
}).catch(err => {
console.log(Error, err.message);
})
I understood you error as I also faced it will trying to connect to mongoDB.
This error is simply popping up because your mongoDB local server failed to connect to remote server probably because you don't shut down the file correctly.
How to fix:
- Go to cmd prompt and change dir to mongo bin file like this (
c:\program files\mongodb\server\4.2\bin
). - Now type mongo and hit enter, you will see error not able to connect to mongodb server.
- Now go to task manager.
- Go to services tab.
- Find mongodb and select it.
- Now open the open service tab at bottom of same screen.
- Again search for mongodb server and select it.
- Now click the green play button saying start service.
That's it , your mongodb server is running again. You can check again in cmd prompt by typing same command.
本文标签: javascriptUnhandledPromiseRejectionWarning MongooseServerSelectionErrorStack Overflow
版权声明:本文标题:javascript - UnhandledPromiseRejectionWarning: MongooseServerSelectionError - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738665763a2105690.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论