Unhandled rejection MongoError: port must be specified

  • A+
Category:Languages

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.

 


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 mongoose@5.1.7 --save 

Let me know if that fixes it.

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: