节点和mongodb的问题-UnhandledPromiseRejectionWarning:MongoNetworkError:连接4

时间:2019-04-15 17:21:14

标签: javascript node.js mongodb mongoose

我试图通过vscode控制台连接到mongobd,它正在工作,但是现在当我输入“ yarn dev”时,它就会出现。

(node:4532) UnhandledPromiseRejectionWarning: MongoNetworkError: connection 4 to cluster0-shard-00-02-jaf8f.mongodb.net:27017 closed
    at TLSSocket.<anonymous> (C:\Users\note-gambali\Desktop\Alexandre\OmniStack\backend\node_modules\mongodb-core\lib\connection\connection.js:352:9)
    at Object.onceWrapper (events.js:281:20)
    at TLSSocket.emit (events.js:193:13)
    at _handle.close (net.js:614:12)
    at TCP.done (_tls_wrap.js:412:7)
(node:4532) 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:4532) [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.

我已经通过mongo地图集建立了连接,但是已经做了测试,但是,添加了以下代码之后:

const express = require('express');
const mongoose = require('mongoose');
const path = require('path');
const cors = require('cors');

const app = express();

app.use(cors());

const server = require('http').Server(app);
const io = require('socket.io')(server);

//he stopped after adding this stretch
io.on("connection", socket => {
    socket.on("connectRoom", box => {
        socket.join(box);
    });
});

mongoose.connect('mongodb+srv://omnistack:omnistack@cluster0-jaf8f.mongodb.net/omnistack?retryWrites=true', {
        useNewUrlParser: true
});

app.use((req, res, next) => {
    req.io = io;

    return next();
});

app.use(express.json());
app.use(express.urlencoded({ extended: true}));
app.use("/files", express.static(path.resolve(__dirname, "..", "tmp")));

app.use(require("./routes"));

server.listen(process.env.PORT || 3333);

1 个答案:

答案 0 :(得分:0)

在Atlas中将您的IP地址列入白名单,可以在“安全性/ IP白名单”标签下找到该选项

enter image description here