From b9e5b0f060431e694f2f2bd38b67f92e97f87545 Mon Sep 17 00:00:00 2001 From: Devookim Date: Fri, 26 Jan 2024 23:06:20 +0900 Subject: [PATCH 1/7] remove callback from QueryCusor.eachAsync --- lib/cursor/queryCursor.js | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/lib/cursor/queryCursor.js b/lib/cursor/queryCursor.js index 9ecbb27cdd1..3ec2b2de3a2 100644 --- a/lib/cursor/queryCursor.js +++ b/lib/cursor/queryCursor.js @@ -277,20 +277,18 @@ QueryCursor.prototype.next = async function next() { * @param {Number} [options.parallel] the number of promises to execute in parallel. Defaults to 1. * @param {Number} [options.batchSize] if set, will call `fn()` with arrays of documents with length at most `batchSize` * @param {Boolean} [options.continueOnError=false] if true, `eachAsync()` iterates through all docs even if `fn` throws an error. If false, `eachAsync()` throws an error immediately if the given function `fn()` throws an error. - * @param {Function} [callback] executed when all docs have been processed * @return {Promise} * @api public * @method eachAsync */ -QueryCursor.prototype.eachAsync = function(fn, opts, callback) { +QueryCursor.prototype.eachAsync = function(fn, opts) { if (typeof opts === 'function') { - callback = opts; opts = {}; } opts = opts || {}; - return eachAsync((cb) => _next(this, cb), fn, opts, callback); + return eachAsync((cb) => _next(this, cb), fn, opts); }; /** From 13906bcf640bc3fa489aadec3041f4392fd92a16 Mon Sep 17 00:00:00 2001 From: Devookim Date: Fri, 26 Jan 2024 23:24:03 +0900 Subject: [PATCH 2/7] remove callback from AggregationCursor.eachAsync --- lib/cursor/aggregationCursor.js | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/lib/cursor/aggregationCursor.js b/lib/cursor/aggregationCursor.js index 9b3e4795722..7c574129d03 100644 --- a/lib/cursor/aggregationCursor.js +++ b/lib/cursor/aggregationCursor.js @@ -219,21 +219,19 @@ AggregationCursor.prototype.next = async function next() { * @param {Function} fn * @param {Object} [options] * @param {Number} [options.parallel] the number of promises to execute in parallel. Defaults to 1. - * @param {Function} [callback] executed when all docs have been processed * @return {Promise} * @api public * @method eachAsync */ -AggregationCursor.prototype.eachAsync = function(fn, opts, callback) { +AggregationCursor.prototype.eachAsync = function(fn, opts) { const _this = this; if (typeof opts === 'function') { - callback = opts; opts = {}; } opts = opts || {}; - return eachAsync(function(cb) { return _next(_this, cb); }, fn, opts, callback); + return eachAsync(function(cb) { return _next(_this, cb); }, fn, opts); }; /** From 62a0cdb3738057792d5d60978d1bead5688f58a2 Mon Sep 17 00:00:00 2001 From: Devookim Date: Fri, 26 Jan 2024 23:28:51 +0900 Subject: [PATCH 3/7] add doc for AggregationCursor.eachAsync --- lib/cursor/aggregationCursor.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/cursor/aggregationCursor.js b/lib/cursor/aggregationCursor.js index 7c574129d03..39018e823ee 100644 --- a/lib/cursor/aggregationCursor.js +++ b/lib/cursor/aggregationCursor.js @@ -219,6 +219,8 @@ AggregationCursor.prototype.next = async function next() { * @param {Function} fn * @param {Object} [options] * @param {Number} [options.parallel] the number of promises to execute in parallel. Defaults to 1. + * @param {Number} [options.batchSize=null] if set, Mongoose will call `fn` with an array of at most `batchSize` documents, instead of a single document + * @param {Boolean} [options.continueOnError=false] if true, `eachAsync()` iterates through all docs even if `fn` throws an error. If false, `eachAsync()` throws an error immediately if the given function `fn()` throws an error. * @return {Promise} * @api public * @method eachAsync From 07d2cdd531188cf163bb209fa795a0ae980b9b86 Mon Sep 17 00:00:00 2001 From: Devookim Date: Fri, 26 Jan 2024 23:38:57 +0900 Subject: [PATCH 4/7] add error for eachAsync --- lib/cursor/aggregationCursor.js | 3 +++ lib/cursor/queryCursor.js | 3 +++ 2 files changed, 6 insertions(+) diff --git a/lib/cursor/aggregationCursor.js b/lib/cursor/aggregationCursor.js index 39018e823ee..25d81b20803 100644 --- a/lib/cursor/aggregationCursor.js +++ b/lib/cursor/aggregationCursor.js @@ -227,6 +227,9 @@ AggregationCursor.prototype.next = async function next() { */ AggregationCursor.prototype.eachAsync = function(fn, opts) { + if (typeof arguments[2] === 'function') { + throw new MongooseError('AggregationCursor.prototype.eachAsync() no longer accepts a callback'); + } const _this = this; if (typeof opts === 'function') { opts = {}; diff --git a/lib/cursor/queryCursor.js b/lib/cursor/queryCursor.js index 3ec2b2de3a2..4f69d0d014a 100644 --- a/lib/cursor/queryCursor.js +++ b/lib/cursor/queryCursor.js @@ -283,6 +283,9 @@ QueryCursor.prototype.next = async function next() { */ QueryCursor.prototype.eachAsync = function(fn, opts) { + if (typeof arguments[2] === 'function') { + throw new MongooseError('QueryCursor.prototype.eachAsync() no longer accepts a callback'); + } if (typeof opts === 'function') { opts = {}; } From 3f3aa09b088125a305268792565cd53b58eec011 Mon Sep 17 00:00:00 2001 From: Devookim Date: Fri, 26 Jan 2024 23:41:02 +0900 Subject: [PATCH 5/7] fix "if" for QueryCursor.next --- lib/cursor/queryCursor.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/cursor/queryCursor.js b/lib/cursor/queryCursor.js index 4f69d0d014a..ff59f6aeba8 100644 --- a/lib/cursor/queryCursor.js +++ b/lib/cursor/queryCursor.js @@ -243,7 +243,7 @@ QueryCursor.prototype.rewind = function() { */ QueryCursor.prototype.next = async function next() { - if (arguments[0] === 'function') { + if (typeof arguments[0] === 'function') { throw new MongooseError('QueryCursor.prototype.next() no longer accepts a callback'); } return new Promise((resolve, reject) => { From 47f40436473675e5be7f4ee34dc9500093fa4e9b Mon Sep 17 00:00:00 2001 From: Devookim Date: Fri, 26 Jan 2024 23:43:26 +0900 Subject: [PATCH 6/7] add eachAsync to migrate_to_7 --- docs/migrating_to_7.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docs/migrating_to_7.md b/docs/migrating_to_7.md index d9526322f4b..1ad1ccbfa7f 100644 --- a/docs/migrating_to_7.md +++ b/docs/migrating_to_7.md @@ -87,6 +87,7 @@ They always return promises. * `Aggregate.prototype.exec` * `Aggregate.prototype.explain` * `AggregationCursor.prototype.close` +* `AggregationCursor.prototype.eachAsync` * `Connection.prototype.startSession` * `Connection.prototype.dropCollection` * `Connection.prototype.createCollection` @@ -138,6 +139,7 @@ They always return promises. * `Query.prototype.exec` * `QueryCursor.prototype.close` * `QueryCursor.prototype.next` +* `QueryCursor.prototype.eachAsync` If you are using the above functions with callbacks, we recommend switching to async/await, or promises if async functions don't work for you. If you need help refactoring a legacy codebase, [this tool from Mastering JS callbacks to async await](https://masteringjs.io/tutorials/tools/callback-to-async-await) using ChatGPT. From 827e6ad1e19e18c561ae1bb540607d7ca5449f1e Mon Sep 17 00:00:00 2001 From: Devookim Date: Fri, 26 Jan 2024 23:46:58 +0900 Subject: [PATCH 7/7] add .next to migrate_to_7 --- docs/migrating_to_7.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/migrating_to_7.md b/docs/migrating_to_7.md index 1ad1ccbfa7f..c0500576c28 100644 --- a/docs/migrating_to_7.md +++ b/docs/migrating_to_7.md @@ -87,6 +87,7 @@ They always return promises. * `Aggregate.prototype.exec` * `Aggregate.prototype.explain` * `AggregationCursor.prototype.close` +* `AggregationCursor.prototype.next` * `AggregationCursor.prototype.eachAsync` * `Connection.prototype.startSession` * `Connection.prototype.dropCollection`