我正在关注以下教程,因为我想从MongoDB和MERN堆栈开始:

  • https://medium.com/@beaucarnes/learn-the-mern-stack-by-building-an-exercise-tracker-mern-tutorial-59c13c1237a1
  • https://www.youtube.com/watch?v=7CqJlxBYj-M

  • 一切正常,直到我与数据库的连接失败为止。
    我检查了用户名/密码一百万次,我复制并粘贴了原始文件,以防万一我遇到语法错误。这是错误:
    (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.
    

    这是我的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}`);
    });
    
    

    由于错误是关于未处理的 promise ,因此我在 Mongoose 连接中还尝试了以下操作:
      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 <monitor> to 52.212.0.151:27017 closed'更改为34.249.129.6:27017
    我尝试过的另一件事是删除集群并创建一个新集群,但同样的事情发生了
    我正在使用Windows和 Node v10.16.3

    希望你们能帮助我或给我建议。

    最佳答案

    尝试这个
    似乎您正在使用MongoDB map 集

    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即可!!

    关于javascript - UnhandledPromiseRejectionWarning : MongooseServerSelectionError,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/60563988/

    10-09 18:07