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

feat: upgrade mongodb -> 6.8.0, handle throwing error on closed cursor in Mongoose #14813

Merged
merged 10 commits into from
Aug 21, 2024
Merged
5 changes: 5 additions & 0 deletions lib/cursor/queryCursor.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ function QueryCursor(query) {
this.cursor = null;
this.skipped = false;
this.query = query;
this._closed = false;
const model = query.model;
this._mongooseOptions = {};
this._transforms = [];
Expand Down Expand Up @@ -229,6 +230,7 @@ QueryCursor.prototype.close = async function close() {
}
try {
await this.cursor.close();
this._closed = true;
this.emit('close');
} catch (error) {
this.listeners('error').length > 0 && this.emit('error', error);
Expand Down Expand Up @@ -266,6 +268,9 @@ QueryCursor.prototype.next = async function next() {
if (typeof arguments[0] === 'function') {
throw new MongooseError('QueryCursor.prototype.next() no longer accepts a callback');
}
if (this._closed) {
throw new MongooseError('Cannot call `next()` on a closed cursor');
}
return new Promise((resolve, reject) => {
_next(this, function(error, doc) {
if (error) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"dependencies": {
"bson": "^6.7.0",
"kareem": "2.6.3",
"mongodb": "6.7.0",
"mongodb": "6.8.0",
"mpath": "0.9.0",
"mquery": "5.0.0",
"ms": "2.1.3",
Expand Down
3 changes: 2 additions & 1 deletion test/query.cursor.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,8 @@ describe('QueryCursor', function() {
await cursor.next();
assert.ok(false);
} catch (error) {
assert.equal(error.name, 'MongoCursorExhaustedError');
assert.equal(error.name, 'MongooseError');
assert.ok(error.message.includes('closed cursor'), error.message);
}
});
});
Expand Down
Loading