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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With