Mongo网络错误:连接0到本地主机

时间:2018-06-30 15:51:52

标签: javascript node.js mongodb express

我正在尝试为node.js项目设置mongodb,但是每次尝试连接到mongodb时,都会不断遇到此错误。这是错误:

(node:4564) DeprecationWarning: current URL string parser is deprecated, and will be removed in a future version. To use the new parser, pass option { useN
ewUrlParser: true } to MongoClient.connect.
GET /connection-test 304 78.511 ms - -
GET /stylesheets/style.css 304 2.503 ms - -
/Users/NuriAmari/Developer/MajorIncidentManagement/node_modules/mongodb/lib/operations/mongo_client_ops.js:439
      throw err;
      ^

AssertionError [ERR_ASSERTION]: null == 'MongoNetworkError: failed to connect to server [localhost:27017] on first connect [MongoNetworkError: connection 0

 to localhost
    at /Users/NuriAmari/Developer/MajorIncidentManagement/routes/index.js:15:12
    at err (/Users/NuriAmari/Developer/MajorIncidentManagement/node_modules/mongodb/lib/utils.js:415:14)
    at executeCallback (/Users/NuriAmari/Developer/MajorIncidentManagement/node_modules/mongodb/lib/utils.js:404:25)
    at err (/Users/NuriAmari/Developer/MajorIncidentManagement/node_modules/mongodb/lib/operations/mongo_client_ops.js:284:21)
    at connectCallback (/Users/NuriAmari/Developer/MajorIncidentManagement/node_modules/mongodb/lib/operations/mongo_client_ops.js:240:5)
    at process.nextTick (/Users/NuriAmari/Developer/MajorIncidentManagement/node_modules/mongodb/lib/operations/mongo_client_ops.js:436:7)
    at _combinedTickCallback (internal/process/next_tick.js:131:7)
    at process._tickCallback (internal/process/next_tick.js:180:9)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! majorincidentmanagement@0.0.0 start: `node ./bin/www`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the majorincidentmanagement@0.0.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/NuriAmari/.npm/_logs/2018-06-30T15_43_45_033Z-debug.log`

编辑:这是我的代码原谅我的无知。这是文件处理快递生成器尚未生成的所有路由的文件。我安装了最新的稳定版本(4.0.0),项目中的package.json文件显示"^3.1.0"并运行mongod --version会给我v3.6.5

var express = require('express');
var router = express.Router();
const MongoClient = require('mongodb').MongoClient;
const assert = require("assert");

const url = 'mongodb://localhost:27017/test';

/* GET home page. */
router.get('/', function(req, res, next) {
  res.render('index', { title: 'Express' });
});

router.get('/connection-test', function(req, res, next) {
  MongoClient.connect(url, function(err, client) {
    assert.equal(null, err);
    console.log("Connected :)");
    client.close();
  })
  res.render('index', { title: 'Express' });
});

module.exports = router;

我已经看到类似的问题,但存在某种超时错误,但似乎都无法解决我的问题。我看到了上面的警告,但是即使MongoDB官方指南也没有处理它,因此我认为这不是问题。我可以连接到在终端上运行客户端的MongoDB,但无法通过项目中的节点执行此操作。任何帮助将不胜感激。

0 个答案:

没有答案
相关问题