尝试使用节点连接到MongoDB时出错

时间:2018-08-09 15:01:03

标签: node.js mongodb mean-stack

我刚刚开始了解MEAN堆栈,并且尝试连接到我创建的mongoDB数据库时遇到了此错误

PS C:\Angular\practice\NodeJS> node db.js
Error in DB connection: {}
(node:4416) DeprecationWarning: current URL string parser is deprecated, and 

will be removed in a future version. To use the new parser,
 pass option { useNewUrlParser: true } to MongoClient.connect.
(node:4416) UnhandledPromiseRejectionWarning: Error: Invalid port (larger than 65535) with hostname
at parseConnectionString (C:\Angular\practice\NodeJS\node_modules\mongodb\lib\url_parser.js:155:11)
at parseHandler (C:\Angular\practice\NodeJS\node_modules\mongodb\lib\url_parser.js:129:14)
at module.exports (C:\Angular\practice\NodeJS\node_modules\mongodb\lib\url_parser.js:25:12)
at deprecated (internal/util.js:47:15)
at connect (C:\Angular\practice\NodeJS\node_modules\mongodb\lib\operations\mongo_client_ops.js:179:3)
at connectOp (C:\Angular\practice\NodeJS\node_modules\mongodb\lib\operations\mongo_client_ops.js:283:3)
at executeOperation (C:\Angular\practice\NodeJS\node_modules\mongodb\lib\utils.js:420:24)
at MongoClient.connect (C:\Angular\practice\NodeJS\node_modules\mongodb\lib\mongo_client.js:168:10)
at Promise (C:\Angular\practice\NodeJS\node_modules\mongoose\lib\connection.js:499:12)
at new Promise (<anonymous>)
at NativeConnection.Connection.openUri (C:\Angular\practice\NodeJS\node_modules\mongoose\lib\connection.js:496:19)
 at Mongoose.connect (C:\Angular\practice\NodeJS\node_modules\mongoose\lib\index.js:230:15)
at Object.<anonymous> (C:\Angular\practice\NodeJS\db.js:3:10)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
(node:4416) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async f
unction without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 3)
(node:4416) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not ha
ndled will terminate the Node.js process with a non-zero exit code.

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

我对如何得到错误感到困惑:端口无效,因为我对mongoDB连接使用了默认设置。

这是db.js代码

const mongoose = require('mongoose');

mongoose.connect('mongodb://localhost:270107/CRUDDB', (err)=> {
  if(!err){
    console.log('MongoDB connection is successful');
  }

  else{
    console.log('Error in DB connection: ' + JSON.stringify(err,undefined, 2));
  }
});

module.exports = mongoose;

请为这个问题的人提供帮助,在此先感谢

0 个答案:

没有答案