2015-10-16 114 views
1

我在使用猫鼬连接到Mongodb时遇到了一个问题。
问题是当我脱机显示连接失败localhost:27017 但是当我在线它连接成功。
供参考的代码如下:
猫鼬连接在本地主机脱机时失败

mongoose.connect('mongodb://localhost/database'); 
var db = mongoose.connection; 
db.on('error', function (err) { 
console.log('connection error', err); 
}); 
db.once('open', function() { 
console.log('connected.'); 
}); 

这里是两种情况的屏幕截图。 first command fails when there was no internet and second one connects successfully

我需要在我的系统中进行一些配置才能使其工作,或者它只是以这种方式工作。

+0

可能重复[Mongoose无法连接没有Internet](http://stackoverflow.com/questions/29178484/mongoose-cant-connect-without-internet) – Yaacov

回答

3

这是Mongoose Can't Connect Without Internet

重复总之,只是在连接字符串中127.0.0.1更换localhost。问题在于Windows,请参阅nodejs cannot resolve 'localhost' on windows

+0

谢谢。它显示你不能接受答案在4分钟。 –

+0

不要担心,但在将来您应该在提交问题前彻底研究您的问题。我简直就是用你的问题来搜索你的问题,并把它作为第一个结果复制出来,你可以很容易地做到这一点。 – Svenskunganka

+0

我做到了,但我无法正确说出它。 –