-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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
Cursor 'close' event emitted twice #10876
Comments
I've found this PR https://github.com/Automattic/mongoose/pull/8834/files |
close only fires once for me const mongoose = require('mongoose');
const testSchema = new mongoose.Schema({
name: String
});
const Test = mongoose.model('Test', testSchema);
async function run() {
await mongoose.connect('mongodb://localhost:27017/', {useNewUrlParser: true,
useUnifiedTopology: true,});
await mongoose.connection.dropDatabase();
const cursor = await Test.find({}).cursor();
cursor.on('data', (obj) => {
cursor.pause();
// simulate the async actions using obj
setTimeout(() => cursor.resume(), 50);
});
cursor.on('close', () => console.log('close'));
}
run();
|
you don't have any document in collection, the pause/resume is not called if no documents. Have a look on my tests that I have added. |
const mongoose = require('mongoose');
const testSchema = new mongoose.Schema({
name: String
});
const Test = mongoose.model('Test', testSchema);
async function run() {
await mongoose.connect('mongodb://localhost:27017/', {useNewUrlParser: true,
useUnifiedTopology: true,});
await mongoose.connection.dropDatabase();
await Test.create({name: 'Test'});
await Test.create({name: 'Other Test'})
const cursor = await Test.find({}).cursor();
cursor.on('data', (obj) => {
cursor.pause();
// simulate the async actions using obj
setTimeout(() => cursor.resume(), 50);
});
cursor.on('close', () => console.log('close'));
}
run(); |
@vkarpov15 I've created a PR for version 5.x |
Do you want to request a feature or report a bug?
Bug
What is the current behavior?
When the cursor is used with pause/resume the event 'close' is emitted twice.
If the current behavior is a bug, please provide the steps to reproduce.
The code below will get two 'close' in the console;
What is the expected behavior?
The expected behavior is to receive only one 'close' event.
What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
node - 14.18.0
mongoose - 5.13.11
MongoDB - 4.4.9
The text was updated successfully, but these errors were encountered: