1

I am facing a issue while connecting to Mongodb using mongoose.
The problem is when i am offline it shows connection failed to localhost:27017 but when i am online it connects successfully.
The code for reference is below:

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.');
});

And here is the screenshot of both the situation. first command fails when there was no internet and second one connects successfully

Do I need to make some configuration in my system to make it work or It just work this way.

Shvet Chakra
  • 1,043
  • 10
  • 21
  • Possible duplicate of [Mongoose Can't Connect Without Internet](http://stackoverflow.com/questions/29178484/mongoose-cant-connect-without-internet) – Yaacov Apr 24 '17 at 18:14

1 Answers1

4

This is a duplicate of Mongoose Can't Connect Without Internet

Anyhow, just replace localhost with 127.0.0.1 in the connection string. The issue lies within Windows, see nodejs cannot resolve 'localhost' on windows

Community
  • 1
  • 1
Sven
  • 5,155
  • 29
  • 53
  • Thanks . Its showing you can't accept answer in 4 mins. – Shvet Chakra Oct 16 '15 at 11:33
  • Don't worry about it, but in the future you should thoroughly research your issue before submitting a question. I literally just googled your question and got that duplicate as first result, you could easily have done the same. – Sven Oct 16 '15 at 11:34
  • I did it but I have not able to phrase it properly. – Shvet Chakra Oct 16 '15 at 11:39