【问题标题】:MongoDB connections on Windows 2008 r2 error: getaddrinfo ENOTFOUNDWindows 2008 r2 上的 MongoDB 连接错误:getaddrinfo ENOTFOUND
【发布时间】:2016-07-22 09:36:07
【问题描述】:

我正在编写一份 MEAN 工作申请,但遇到了障碍。我的 index.js 文件,其中包含我的 mongo db 连接信息,似乎是错误的,我正在挠头寻找解决方案。不幸的是,我看到的所有类似问题都在'nix 方面。我不是很精通 Windows,所以如果这是一个愚蠢的问题,我很抱歉。

当前 index.js 文件:

var express = require('express');
var mongoose = require('mongoose');
var bodyParser = require('body-parser');
var methodOverride = require('method-override');
var _ = require('lodash');
var dbURI = 'mongodb:127.0.0.1\d$\db\pclistapp';
// Create the app
var app = express();

// Add middleware necessayr for the REST API
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());
app.use(methodOverride('X-HTTP-Method_Override'));

// Connect to MongoDB
mongoose.connect(dbURI);
//var db = mongoose.connection;

// CONNECTION EVENTS
// When successfully connected
mongoose.connection.on('connected', function () {  
  console.log('Mongoose default connection open to ' + dbURI);
}); 

// If the connection throws an error
mongoose.connection.on('error',function (err) {  
  console.log('Mongoose default connection error: ' + err);
}); 

// When the connection is disconnected
mongoose.connection.on('disconnected', function () {  
  console.log('Mongoose default connection disconnected'); 
});

// If the Node process ends, close the Mongoose connection 
process.on('SIGINT', function() {  
  mongoose.connection.close(function () { 
    console.log('Mongoose default connection disconnected through app termination'); 
    process.exit(0); 
  }); 
});

// Test connection
mongoose.connection.once('open', function() {
    console.log('Listening on port 3000... ');
    app.listen(3000);
})

当我尝试运行 node index.js 时的输出是 MonogError: getaddrinfo ENOTFOUND mongodb mongodb:127。数据库当前在端口 27017 上运行。

我试过了

mongodb:\\"PCname"\d$\db\pclistapp
mongodb:\\localhost\d$\db\pclistapp
mongodb:"PCname"\d$\db\pclistapp
mongodb:localhost\d$\db\pclistapp 

它们似乎都不起作用。我知道 UNC 适用于 \"PCname"\d$\db\pclistapp 所以我不确定问题是什么。

DB Connection Issue

DB Operational

【问题讨论】:

    标签: node.js mongodb mongoose mean-stack meanjs


    【解决方案1】:

    花了很长时间,但错误很简单。我重新编写了代码以压缩错误,它不是我需要连接的 UNC,而是通过 HTTP 连接的。解决这个问题,我就去参加比赛了。

    我真傻。

    代码:

    var mongoURI = "mongodb://localhost:27017/pclistapp";
    var MongoDB = mongoose.connect(mongoURI).connection;
    MongoDB.on('error', function(err) { console.log(err.message); });
    MongoDB.once('open', function() {
      console.log("mongodb connection open");
    });
    

    【讨论】:

      猜你喜欢
      • 2016-04-16
      • 2017-10-16
      • 1970-01-01
      • 2018-03-03
      • 2020-11-10
      • 2014-10-20
      • 1970-01-01
      • 2018-05-24
      • 2016-04-20
      相关资源
      最近更新 更多