Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to check connection error in mongojs #149

Closed
rohittailor opened this issue Sep 1, 2014 · 14 comments
Closed

How to check connection error in mongojs #149

rohittailor opened this issue Sep 1, 2014 · 14 comments

Comments

@rohittailor
Copy link

Hi,

How to check connection error in mongojs. When we run the app and mongodb is up than its working fine. When we stop mongodb, it throws an exception. Can we handle connection errors in mongojs ?

@rohittailor
Copy link
Author

Getting following error:

events.js:72
throw er; // Unhandled 'error' event
^
Error: failed to connect to [127.0.0.1:27017]
at null. (C:\Rohit\Research\No
at EventEmitter.emit (events.js:106:17)
at null. (C:\Rohit\Research\No
at EventEmitter.emit (events.js:98:17)
at Socket. (C:\Rohit\Research
at Socket.EventEmitter.emit (events.js:95
at net.js:440:14
at process._tickCallback (node.js:415:13)

@sorribas
Copy link
Collaborator

sorribas commented Sep 2, 2014

Hello @rohittailor

You can do something like this.

var mongojs = require('mongojs');
var db = mongojs('test', ['a']);

...

db.on('error', function() {
  console.log('we had an error.');
});

And then it shouldn't throw. Let me know if you have any more problems.

@rohittailor
Copy link
Author

Hi Sorribas,

I implemented that code and its working when mongodb up and we stop mongodb. But when mongodb is not running than i am getting same error and its not going in error callback.

Please help.

Thanks,
Rohit

@sorribas
Copy link
Collaborator

sorribas commented Sep 8, 2014

It's true that it doesn't emit the error when the database is not running (we'll fix that), but I don't get the error thrown. What version of mongojs are you running?

@ghost
Copy link

ghost commented Sep 26, 2014

Hi Sorribas,

Is this the same issue - ie on startup if the database doesn't exist I'm not getting an an error?

var mongojs = require('mongojs');
var db = mongojs('nonexistentdb', ['a']);

...

db.on('error', function() {
  console.log('we had an error.');
});

Above error not being logged if connection fails.

Many thanks,
Ben

@bluedusk
Copy link

Hi , I ran into this same problem with mongojs 1.3.0 on Windows 7 , I cant get any event when mongodb is not running or when I stop it.

var mongojs = require('mongojs');
var db = mongojs('test', ['a']);

db.on('error', function() {
  console.log('we had an error.');
});

@Sparragus
Copy link

I have the same issue as @bluedusk. If I don't have a mongodb server running, the app simply fails silently and exits. It should emit the error event or throw. It does neither.

Specs:
OSX 10.10.5
node v2.2.1
mongojs 1.3.0
mongodb v3.0.6

@sergiolc
Copy link

sergiolc commented Oct 5, 2015

I have the same issue as @Sparragus.

mongojs 1.3.0

@julio-coelho
Copy link

Same here. If the server was not running on 'connect', no error is thrown.
A callback function would be nice.

@imjul1an
Copy link

same here.

@rafajaw
Copy link

rafajaw commented Nov 25, 2015

Is it going to be fixed? it's a showstopper for me. I saw some topic saying to use a connectionTimeout parameter like this:

var db  =  mongojs( db_conn, ['bots'], {connectionTimeout: 3000} );
db.on('timeout', function (err) {
    console.log('database timeout');
});

but it is still not working.

@iMoses
Copy link

iMoses commented Nov 26, 2015

Nothing worse than finding the issue you're struggling with in github only to discover it should have been fixed a year ago :)

@saintedlama
Copy link
Collaborator

In version 2.0.0 you can register an error event listener with

var db  =  mongojs( db_conn, ['bots'], {connectionTimeout: 3000} );
db.on('error', function (err) {
    console.log('Connection errored', err);
});

@amado-saladino
Copy link

That did not work for me, it throws the error and logged (app crashed)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests