【问题标题】:An error when connecting to mLab database with nodejs and deprecation warnings使用 nodejs 和弃用警告连接到 mLab 数据库时出错
【发布时间】:2018-06-29 05:37:41
【问题描述】:

我还是 node 新手,目前使用 node 版本 10.5.0 和 mongoose 版本 5.1.7。我已经仔细检查了有关 mlab 的现有问题,但没有一个解决我的问题。我正在尝试连接到 mLab,这是我的代码:

const mongoose = require('mongoose');
const dbUri = 'mongodb://username:password@ds121371.mlab.com:21371/kucubookstore';

// connecting to mlab mongodb
mongoose.connect(dbUri, function(error) {
  console.log('Connection Successful', error);
});

但是收到以下错误,我不明白。请帮忙:

E:\Documents\Computer and Coding\PlayGround\PROJECTS\Catalogue>node test/connection.js
Connection Successful Error: Missing delimiting slash between hosts and options
    at parseConnectionString (E:\Documents\Computer and Coding\PlayGround\PROJECTS\Catalogue\node_modules\mongodb\lib\url_parser.js:164:11)
    at parseHandler (E:\Documents\Computer and Coding\PlayGround\PROJECTS\Catalogue\node_modules\mongodb\lib\url_parser.js:129:14)
    at module.exports (E:\Documents\Computer and Coding\PlayGround\PROJECTS\Catalogue\node_modules\mongodb\lib\url_parser.js:25:12)
    at connect (E:\Documents\Computer and Coding\PlayGround\PROJECTS\Catalogue\node_modules\mongodb\lib\mongo_client.js:880:3)
    at connectOp (E:\Documents\Computer and Coding\PlayGround\PROJECTS\Catalogue\node_modules\mongodb\lib\mongo_client.js:270:3)
    at executeOperation (E:\Documents\Computer and Coding\PlayGround\PROJECTS\Catalogue\node_modules\mongodb\lib\utils.js:420:24)
    at MongoClient.connect (E:\Documents\Computer and Coding\PlayGround\PROJECTS\Catalogue\node_modules\mongodb\lib\mongo_client.js:261:10)
    at Promise (E:\Documents\Computer and Coding\PlayGround\PROJECTS\Catalogue\node_modules\mongoose\lib\connection.js:436:12)
    at new Promise (<anonymous>)
    at NativeConnection.Connection.openUri (E:\Documents\Computer and Coding\PlayGround\PROJECTS\Catalogue\node_modules\mongoose\lib\connection.js:433:19)
    at Mongoose.connect (E:\Documents\Computer and Coding\PlayGround\PROJECTS\Catalogue\node_modules\mongoose\lib\index.js:212:15)
    at Object.<anonymous> (E:\Documents\Computer and Coding\PlayGround\PROJECTS\Catalogue\test\connection.js:5:10)
    at Module._compile (internal/modules/cjs/loader.js:702:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:713:10)
    at Module.load (internal/modules/cjs/loader.js:612:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:551:12)
(node:9016) UnhandledPromiseRejectionWarning: Error: Missing delimiting slash between hosts and options
    at parseConnectionString (E:\Documents\Computer and Coding\PlayGround\PROJECTS\Catalogue\node_modules\mongodb\lib\url_parser.js:164:11)
    at parseHandler (E:\Documents\Computer and Coding\PlayGround\PROJECTS\Catalogue\node_modules\mongodb\lib\url_parser.js:129:14)
    at module.exports (E:\Documents\Computer and Coding\PlayGround\PROJECTS\Catalogue\node_modules\mongodb\lib\url_parser.js:25:12)
    at connect (E:\Documents\Computer and Coding\PlayGround\PROJECTS\Catalogue\node_modules\mongodb\lib\mongo_client.js:880:3)
    at connectOp (E:\Documents\Computer and Coding\PlayGround\PROJECTS\Catalogue\node_modules\mongodb\lib\mongo_client.js:270:3)
    at executeOperation (E:\Documents\Computer and Coding\PlayGround\PROJECTS\Catalogue\node_modules\mongodb\lib\utils.js:420:24)
    at MongoClient.connect (E:\Documents\Computer and Coding\PlayGround\PROJECTS\Catalogue\node_modules\mongodb\lib\mongo_client.js:261:10)
    at Promise (E:\Documents\Computer and Coding\PlayGround\PROJECTS\Catalogue\node_modules\mongoose\lib\connection.js:436:12)
    at new Promise (<anonymous>)
    at NativeConnection.Connection.openUri (E:\Documents\Computer and Coding\PlayGround\PROJECTS\Catalogue\node_modules\mongoose\lib\connection.js:433:19)
    at Mongoose.connect (E:\Documents\Computer and Coding\PlayGround\PROJECTS\Catalogue\node_modules\mongoose\lib\index.js:212:15)
    at Object.<anonymous> (E:\Documents\Computer and Coding\PlayGround\PROJECTS\Catalogue\test\connection.js:5:10)
    at Module._compile (internal/modules/cjs/loader.js:702:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:713:10)
    at Module.load (internal/modules/cjs/loader.js:612:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:551:12)
(node:9016) 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:9016) [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.

【问题讨论】:

  • 您是否使用自己的usernamepassword 而不是占位符username:password
  • 您的密码中有@等特殊字符吗?
  • 是的,我的第一个 DB 用户有很多特殊字符。但我创建的新用户只有一个句号
  • 谢谢。请注意,我的声望很低,我的投票不算数。同时,请检查其他错误并为我提出可能的原因。
  • 您可以针对遇到的新错误提出新问题。

标签: javascript node.js mongodb mongoose mlab


【解决方案1】:

当您的密码包含在 URL 中具有特殊含义的特殊字符(如 @)时,会发生此错误。尝试将密码更改为简单的密码,例如仅字母数字字符转义这些字符 (https://www.w3schools.com/tags/ref_urlencode.asp)。

例如,如果您将密码设置为one@two,则将其更改为one%40two

如果您认为您的互联网提供商无法与该服务器连接,请尝试使用一些 VPN 进行连接。

【讨论】:

  • 刚刚下载了一个VPN软件,现在一切正常。没有更多错误。
  • 我讨厌要求它。但是你可以接受这个答案,这个勾号解决了你的问题。
  • 感谢赛义夫乌拉赫曼!一个提示,如果我想要一个安全的密码,我可以使用 strongpasswordgenerator.com 并标记“避免在编程中使用标点符号”。
  • @fedeteka 如果你不这样做会更好,因为如果他们维护一个包含在其网站上生成的所有随机密码的数据库,然后网站的创建者可以使用该数据库来对您的数据库执行字典攻击。您可以创建自己的强密码,类似于您在 strongpasswordgenerator.com 上看到的密码,但不要复制您在他们的网站上生成的确切密码。
  • @Saif Ur Rahman 我知道,但来自网站:“本网站不会通过 Internet 发送新密码。本网站使用 JavaScript 在您的浏览器中生成新密码 - 请随时查看网页的标记。为确保在线安全,请确保您已升级浏览器。"
猜你喜欢
  • 1970-01-01
  • 2020-09-15
  • 1970-01-01
  • 2021-03-05
  • 1970-01-01
  • 1970-01-01
  • 2015-01-15
  • 2012-05-24
  • 2021-10-26
相关资源
最近更新 更多