LondonRob
LondonRob

Reputation: 78723

Unhandled rejection MongoError: port must be specified

According to the Mongoose docs you can connect to a MongoDB with a default port of 27017.

My mongod output ends with:

 I NETWORK  [initandlisten] waiting for connections on port 27017

But when I try and connect to my database like this:

const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/myapp');

I get

Unhandled rejection MongoError: port must be specified

What is happening here?

I have Mongoose v5.2.0, node v9.10.0, and MongoDB shell version v4.0.0.

If I change my connection string to include the port mongodb://localhost:27017/myapp then I am able to connect. But I'm not sure why I need this.

Upvotes: 21

Views: 5229

Answers (2)

vkarpov15
vkarpov15

Reputation: 3872

Mongoose maintainer here, sorry about the trouble. We'll fix this issue within the next couple days. Until then, please specify the port in your URI: mongoose.connect('mongodb://localhost:27017/myapp');. We'll also make sure to update the relevant docs.

Upvotes: 29

amit thakur
amit thakur

Reputation: 120

Port should also be specified in mongoose.connect() in the latest version of mongoose (5.2.0):

mongoose.connect("mongodb://localhost:27017/cat_app");

27017 is MongoDB's default port on which mongod is running.

Another solution is to revert to mongoose 5.1.7 which won't cause this issue:

npm install [email protected] --save

Let me know if that fixes it.

Upvotes: 5

Related Questions