更新到 mongoose 5.x 后无法连接到本地 mongo db

问题描述

我已经检查了 Stack 和 Mongoose 文档,但我似乎无法正确理解。正如标题所暗示的,一切都很好,直到我升级了 mongoose 和 connect-mongo 的软件包。

这是我的 database.js:

var mongoose = require('mongoose');
var mongodbUri = require('mongodb-uri');

function encodeMongoURI (urlString) {
    if (urlString) {
      var parsed = mongodbUri.parse(urlString)
      urlString = mongodbUri.format(parsed);
    }
    return urlString;
  }


mongoose.connect(encodeMongoURI(process.env.MONGODB_URI) || 'mongodb://localhost:27017/goGradesDB',//specifies goGradesDB as the db to use locally
                 {
    useNewUrlParser: true,useUnifiedTopology: true,useFindAndModify: false,useCreateIndex: true,}).catch(error => handleError(error));

var db = mongoose.connection;

db.on('error',console.error.bind(console,'MongoDB connection error:'));


module.exports = db;

在查看 mongoose 中的文档并搜索站点后,我尝试了以下操作:

  • 安装了 mongodb-uri 包来解析 uri(虽然我不确定本地数据库是否需要它。我也无法进入我的 heroku 数据库,但这是我正在与支持人员合作的事情。
  • 添加了“useNewURLParser”等标签
  • 将端口号添加到本地数据库地址
  • 将 catch 添加到 mongoose.connect 的末尾

尽管如此,我的终端中不断出现以下错误输出,每当我尝试登录应用程序时,数据库都会超时。这是输出

"The server is running on port 3000!
(node:71951) UnhandledPromiseRejectionWarning: MongoParseError: URI malformed,cannot be parsed" 

以后,

"(node:71951) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block,or by rejecting a promise which was not handled with .catch(). (rejection id: 2)
(node:71951) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future,promise rejections that are not handled will terminate the Node.js process with a non-zero exit code."

任何见解将不胜感激。谢谢!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)