UnhandledPromiseRejectionWarning: MongooseServerSelectionError [英] UnhandledPromiseRejectionWarning: MongooseServerSelectionError

查看:34
本文介绍了UnhandledPromiseRejectionWarning: MongooseServerSelectionError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习这些教程,因为我想从 MongoDB 和 MERN 堆栈开始:

I am following these tutorials as I wanted to start with MongoDB and the MERN stack:

一切都很好,直到我与数据库的连接失败.我检查了用户名/密码百万次,我复制并粘贴了原件,以防我有一些语法错误.这是错误:

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:UsersMarioDesktopReactProjectMongoDBmern-tracker
ode_modulesmongooseliberrorserverSelection.js:22:11)
    at NativeConnection.Connection.openUri (C:UsersMarioDesktopReactProjectMongoDBmern-tracker
ode_modulesmongooselibconnection.js:808:32)
    at Mongoose.connect (C:UsersMarioDesktopReactProjectMongoDBmern-tracker
ode_modulesmongooselibindex.js:333:15)
    at Object.<anonymous> (C:UsersMarioDesktopReactProjectMongoDBmern-trackerserverserver.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.

这是我的 server.js:

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!"))

这给了我下一个错误:

{ [Function: Error] stackTraceLimit: 16, prepareStackTrace: undefined } 'connection <monitor> to 52.212.0.151:27017 closed'
DB Connected!

但尚未连接.

有时在错误中,connection ;到 52.212.0.151:27017 关闭' 更改为 34.249.129.6:27017

Sometimes in the error, the connection <monitor> to 52.212.0.151:27017 closed' changes to 34.249.129.6:27017

我尝试的另一件事是删除集群并创建一个新集群,但同样的事情发生了我正在使用 windows 和 node v10.16.3

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.

推荐答案

试试这个看来您正在使用 MongoDB 图集

Try 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 ));

然后转到您的 MongoDB Atlas -> NetworkAccess-> Edit-> 并添加 Current IP address它有效!!

and go to your MongoDB Atlas -> NetworkAccess-> Edit-> and add Current IP address it works!!

这篇关于UnhandledPromiseRejectionWarning: MongooseServerSelectionError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆