node.js - NodeJS猫鼬连接问题
问题描述
我是 NodeJS 的新手,今天开始了 MongoDb 部分。我观看 NodeJS 视频(录制于 2016 年),它们与猫鼬连接。但它对我不起作用。
代码:
var mongoose = require('mongoose');
mongoose.Promise = require('bluebird');
var mongoDB = "mongodb://localhost/nodedb";
mongoose.connect(mongoDB,{ useNewUrlParser: true } ,function(err,err){
if(err){
console.log("Cant connect");
}else{
console.log("Connected to :" + mongoDB);
}
})
使用这种方法,我可以连接。至少出现在 Console 中:
Connected to :mongodb://localhost/nodedb
但是有一些错误,其中之一是:
Unhandled rejection MongoError: port must be specified
我写 mongodb://localhost:27017/nodedb 。但是现在连不上。问题在哪里?
解决方案
需要指定mongodb端口,默认27017
https://docs.mongodb.com/manual/reference/default-mongodb-port/
我像这样使用猫鼬:
mongoose.connect('mongodb://localhost:27017/somename')
.then((db)=>{console.log(`connected`);})
.catch(error=>console.log(error));
推荐阅读
- sql - SQL 存储过程尝试选择然后将 URL var 放在一起
- sql - SQL 检查字段是否包含相同的字母
- c++ - 为什么 LAPACKE_dsygvd 在更改矩阵大小后返回错误?
- json - 使用 maximo oslc rest api 时如何包含 SR 相关的工作日志长描述?
- c# - 使用 HierarchyId 从 c# 匿名类型获取值
- python - 从 c 指针创建 numpy 数组会使 ipython 内核崩溃
- acumatica - 如何在 Acumatica 次要版本升级期间对 Customization.InvalidCstDocumentXML 进行故障排除?
- c# - 是否可以使用 DataSet.ReadXml 填充 DataSet 而忽略 xml 字段的大小写?
- bash - No JSON object could be decoded | AWS Cloudwatch
- spring - 在远程服务器java上读/写文件