ApplePie
ApplePie

Reputation: 1185

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?

Upvotes: 9

Views: 30769

Answers (11)

nilinswap
nilinswap

Reputation: 1514

If you are a Mac user, this might be a relevant comment. For me it was because server was not running. On running mongodb, I saw in console a message saying there is no /data/db and / is not writable for me. So I used synthetic.conf solution.

  1. sudo mkdir -p /System/Volumes/Data/data/db
  2. sudo chown -R whoami /System/Volumes/Data/data/db`

  3. echo "data\t/System/Volumes/Data/data" | sudo tee -a /etc/synthetic.conf
  4. reboot your system
  5. run mongodb

and it works.

Upvotes: 0

Saloni Yadav
Saloni Yadav

Reputation: 9

enter image description here

MongoDB connection expects you to put URL encoded password. But when I had a password with '.' (xxx.xxx), the url encoding of that remains as dot. However, it still did not work. i changed the password to simple all lower case string and it worked fine.

Not sure why it is not accepting. But this solved the issue for time-being.

Upvotes: 0

If you are using Mongo db atlas go on Network access and set to your current IP address and then re-run your command.

I hope it will work for you.

Upvotes: 0

Haroon
Haroon

Reputation: 1

Just repair the mongodb installed in your system. By clicking on the package you installed you will have an option to repair it.

Upvotes: 0

kruosento
kruosento

Reputation: 7

I had the same issue and solved by changing the database user's username and password to something much more simple like dev:dev and I had an error using it with user-dev:user-dev ... just change it to something simple

Upvotes: 1

Nagender Pratap Chauhan
Nagender Pratap Chauhan

Reputation: 2204

I faced same issue but after a lot of RND. I found that whts the problem so run this command on your terminal.

sudo service mongod start

then run mongo on terminal and look.

Upvotes: 0

Leon S. Kennedy
Leon S. Kennedy

Reputation: 169

First I tried Skitty's solution, and it didn't work for me, but I still keep the solution in my code, and then I changed app.listen(process.env.PORT || 3000); and it worked for me.

Upvotes: 0

Skitty
Skitty

Reputation: 1787

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.

Upvotes: 46

compileRavi
compileRavi

Reputation: 11

Before you launch your NodeJS command, make sure to create the right directories with the right permissions:

> sudo mkdir -p /data/db
> sudo chmod -R go+w /data/db

Upvotes: 1

AbhinavD
AbhinavD

Reputation: 7282

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.

Upvotes: 10

Sayegh
Sayegh

Reputation: 1441

Not sure what system you're on, but try running this if you're on Linux:

mongo

or for macOS:

mongod

and leave it running in a new terminal and then try running the server again

Upvotes: 9

Related Questions