-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
error event not triggered #6131
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
Comments
here's the code I used to test this:
here's my version info and output:
there was a delay between my last console message and the termination of the script. I'm guessing total time around a minute, but I did not attempt to time it. Can you try a trivial example like mine, just to see if it behaves as expected? |
Hi Lineus, Firstly; this is the code I was using (I wasn't using promise chains).
If I copy and paste your example then it works as expected. However, if I use a similar syntax (code below), run the application, and then drop database from the network it is not triggered (because the callback has already run).
So while the callback method will work to catch errors on initial connection; neither that method nor the Event Emitters seem to work when the database loses connection after it has initially connected. Hopefully that makes sense?? P.S.... Thanks for rick rolling me! |
As an additional point - I tried the following code and the
|
I just debugged a little bit and I can see that when the database is dropped from the network the This is in
Is this by design? |
@OmisNomis I apologize for not catching the order of operations in your initial report. losing your connection to mongodb server after initial connection seems to result in a close event being emitted only if useMongoClient !== true. In mongoose 4 :
in all following examples, I kill (^c) mongodb server after connection is made
after adding useMongoClient: true to mongooseOptions in the code above:
since mongoose 5 defaults useMongoClient to true, the behavior is the same:
after learning about (being reminded of) the disconnect event:
|
Thank @lineus but it's not the The event that isn't being triggered is the |
I can't believe I didn't know (forgot?) about the disconnected event! I also can't believe it was in your comment above the whole time and I missed it :) thanks for being so patient while I learn this stuff @OmisNomis. I'll leave it to @vkarpov15 or @varunjayaraman to answer your question. |
Don't worry about it @lineus - we all have those moments, and we all have to learn. I'm happy to do some more debugging my side if it helps; I just thought I'd post it here in case the |
The 'error' event is not emitted in either 4.x or 5.x when mongoose loses connection after initial connection was made. The 'error' event only gets emitted when the initial connection fails. Below is the output I get in 4.x when I kill the underlying mongodb database: $ node gh-6131.js
(node:14513) DeprecationWarning: `open()` is deprecated in mongoose >= 4.11.0, use `openUri()` instead, or set the `useMongoClient` option if using `connect()` or `createConnection()`. See http://mongoosejs.com/docs/connections.html#use-mongo-client
open event emitted!
1
1
1
1
native driver lost connection
close event emitted!
0
0
0
0
0
0
^C
$ And 5.x:
Below is the script: const URI = 'mongodb://localhost:27017/test'
const mongoose = require('mongoose')
mongoose.Promise = global.Promise
const mongooseOptions = { }
mongoose.connection.on('error', function () {
console.log('error event emitted!')
})
mongoose.connection.on('open', function () {
console.log('open event emitted!')
})
mongoose.connection.on('timeout', function () {
console.log('timeout event emitted!')
})
mongoose.connection.on('close', function () {
console.log('close event emitted!')
})
mongoose.connection.on('disconnected', function () {
console.log('native driver lost connection')
})
setInterval(() => {
console.log(mongoose.connection.readyState);
}, 500);
mongoose.connect(URI, mongooseOptions) The 'close' event did change slightly in 5.x, right now we only emit 'close' when you explicitly Can you provide me a link to the mongodb docker image you're using and the steps necessary to "Amend the network to none" in docker? I don't really use docker so there might be some issue there. |
@vkarpov15 - Here's an example using version
If you now try and do the same thing with version |
Isn't this normal? You aren't shutting the database down, youre; changing how the DNS resolves inside your docker network. So the issue isn't really an "error", your connection string was correct when you initially connected, that DNS is now invalid. it's going to timeout in that case |
@varunjayaraman my point is in the previous version it triggered the I think the most important thing is if that's the expected behaviour. If it is, maybe the documentation could be updated to reflect the change? |
Repro steps1. Run (while mongo is running)const URI = 'mongodb://localhost:27017/test';
const mongoose = require('mongoose');
mongoose.Promise = global.Promise;
const mongooseOptions = {
socketTimeoutMS: 1000
};
mongoose.connection.on('error', function() {
console.log('error event emitted!');
});
mongoose.connection.on('open', function() {
console.log('open event emitted!');
});
mongoose.connection.on('timeout', function() {
console.log('timeout event emitted!');
});
mongoose.connection.on('close', function() {
console.log('close event emitted!');
});
mongoose.connection.on('disconnected', function() {
console.log('native driver lost connection');
});
setInterval(() => {
console.log(mongoose.connection.readyState);
}, 500);
mongoose.connect(URI, mongooseOptions); 2. Stop the mongo processResults:
Expected Results:
|
I think this is the same issue as #6244, because with that fix I get the below output: $ node gh-6131.js
open event emitted!
1
1
1
1
native driver lost connection
0
0
0
0
0
0
0
0
0
^C
$ Fix should be in 5.0.12 |
Uh oh!
There was an error while loading. Please reload this page.
Do you want to request a feature or report a bug?
Bug
What is the current behavior?
The error event on
mongoose.connection.on('error')
is never triggered, when it was on previous versions. For example:If the current behavior is a bug, please provide the steps to reproduce.
What is the expected behavior?
The error event is triggered. This is triggered on previous versions (v4.9.5)
Please mention your node.js, mongoose and MongoDB version.
node - V9.2.0
mongoose - 5.0.4
MongoDB - docker latest
The text was updated successfully, but these errors were encountered: