首页 > 解决方案 > 未处理的拒绝 MongoError:必须指定端口

问题描述

根据Mongoose 文档,您可以使用默认端口 27017 连接到 MongoDB。

我的mongod输出以:

 I NETWORK  [initandlisten] waiting for connections on port 27017

但是当我尝试像这样连接到我的数据库时:

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

我明白了

Unhandled rejection MongoError: port must be specified

这里发生了什么?

我有 Mongoose v5.2.0、node v9.10.0 和 MongoDB shell 版本 v4.0.0。

如果我更改我的连接字符串以包含端口,mongodb://localhost:27017/myapp那么我可以连接。但我不确定我为什么需要这个。

标签: node.jsmongoose

解决方案


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.


推荐阅读