Nodejs and Mongo DB - Unhandledpromiserejectionwarning: Mongonetworkerror: Connection 4

Asked

Viewed 1,095 times

0

I have the following connection code with Mongodb, it was working perfect, but now gives an error.

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

io.on("connection", socket => {
    socket.on("connectRoom", box => {
        socket.join(box);
    });
});

mongoose.connect('mongodb+srv://omnistack:[email protected]/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);

Before I gave a 'Yarn dev' and it connected, now appears the following message on the vscode console :

(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. 

1 answer

1


Tbm I’m taking the Rockeat Seat course. I had to sort of deduce the problem and remember the configuration part of the Mongo.

This situation occurred because in the configuration the external IP was added at the time of configuration. However, this IP changes.

As an alternative: 1. Setting up a fixed IP. 2. Using an IP service 3. Mark the option on: Security >> IP Whitelist and mark the option to access from anywhere.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.