Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unhandled rejection MongoError: port must be specified

Tags:

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.

like image 573
LondonRob Avatar asked Jul 03 '18 13:07

LondonRob


2 Answers

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.

like image 53
vkarpov15 Avatar answered Sep 28 '22 08:09

vkarpov15


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.

like image 40
amit thakur Avatar answered Sep 28 '22 10:09

amit thakur