LearnBoost/mongoose — Issue #759: Connection error event does not emit unless there is a parameter in the callback.
Author: thetrippy
Created: 2012-03-04 20:57:16+00:00
State: open

Not really sure if this is considered a bug or not, because in a real world scenario you wouldnt handle the error without using the error messages in the callback. But, I was just testing and at first I thought that the error callback wasnt working, but finally figured out that it was just not working if the callback has an empty argument list. Generally in javascript I assume that the arguments of a callback are optional and can be named however, And that lead me to believe there was a bug in this event. But, it turns out that there is just an interesting conditional in connection.js that somehow requires that the function contain an argument.

```javascript
// line 166 connection.jsjjj
if (self._events && self._events.error && self._events.error.length) {
```

```javascript
/* this code does not work */
mongoose = require("mongoose")

mongoose.connection.on("error",function() {
console.log(err)
console.log("This statement does not execute, but there is no mongodb server to connect to.")
})

mongoose.connect("mongodb://localhost/testbug")
```

```javascript
/* this code will work */
mongoose = require("mongoose")

mongoose.connection.on("error",function(err) {
console.log(err)
console.log("Hello from error event! ")
})

mongoose.connect("mongodb://localhost/testbug")
```