Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UnhandledPromiseRejectionWarning: MongoNetworkError: failed to connect to server [localhost:27017] on first connect [MongoNetworkError

I tried to run the following command:

node index.js

However, I get the following from my terminal:

    success connection to port 3000
(node:16767) UnhandledPromiseRejectionWarning: MongoNetworkError: failed to connect to server [localhost:27017] on first connect [MongoNetworkError
: connect ECONNREFUSED 127.0.0.1:27017]
    at Pool.<anonymous> (/Users/hatchery/Documents/nodejs/fxexpress/node_modules/mongoose/node_modules/mongodb-core/lib/topologies/server.js:503:11
)
    at emitOne (events.js:116:13)
    at Pool.emit (events.js:211:7)
    at Connection.<anonymous> (/Users/hatchery/Documents/nodejs/fxexpress/node_modules/mongoose/node_modules/mongodb-core/lib/connection/pool.js:326:12)
    at Object.onceWrapper (events.js:317:30)
    at emitTwo (events.js:126:13)
    at Connection.emit (events.js:214:7)
    at Socket.<anonymous> (/Users/hatchery/Documents/nodejs/fxexpress/node_modules/mongoose/node_modules/mongodb-core/lib/connection/connection.js:245:50)
    at Object.onceWrapper (events.js:315:30)
    at emitOne (events.js:116:13)
    at Socket.emit (events.js:211:7)
    at emitErrorNT (internal/streams/destroy.js:64:8)
    at _combinedTickCallback (internal/process/next_tick.js:138:11)
    at process._tickCallback (internal/process/next_tick.js:180:9)
(node:16767) 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:16767) [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.

I am not sure why I am getting this error. Could anyone advice what to do?

like image 269
ApplePie Avatar asked Apr 24 '18 13:04

ApplePie


2 Answers

If you are using mongoDB.Atlas go to network access and set Whitelist Entry: 0.0.0.0/0. enter image description here

enter image description here

Re-run your command.

like image 128
Skitty Avatar answered Oct 26 '22 10:10

Skitty


While the above solution might work, it is better to understand on how to debug and what the error says.

You got UnhandledPromiseRejectionWarning, because in your code, you must not be handling the promise rejection case where you are connection to MongoDB.

Next, the node server failed to connect to the mongodb. This could be because of several reasons. One simple case as mentioned by @Sayegh, the server itself is not running. Could be other reason like wrong port etc, server not accepting incoming connections etc.

How to debug. First step is to figure out if the process is running or not. One simple way would be to run this command ps aux | grep mongo which list all processes from all users with mongo as a text. If this was true and still unable to connect, look for port. If still unable to connect, check if the server accepts incoming connection and take it from there.

like image 31
AbhinavD Avatar answered Oct 26 '22 10:10

AbhinavD