Skip to content

Commit

Permalink
fix(aggregate): handle calling aggregate() before initial connectio…
Browse files Browse the repository at this point in the history
…n succeeds

Fix #10722
Re: #10610
  • Loading branch information
vkarpov15 committed Sep 18, 2021
1 parent f0e6126 commit 4e2f7bf
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 26 deletions.
45 changes: 20 additions & 25 deletions lib/aggregate.js
Expand Up @@ -721,9 +721,14 @@ Aggregate.prototype.explain = function(callback) {

this.options.explain = true;

model.collection.
aggregate(this._pipeline, this.options || {}).
explain((error, result) => {
model.collection.aggregate(this._pipeline, this.options, (error, cursor) => {
if (error != null) {
const _opts = { error: error };
return model.hooks.execPost('aggregate', this, [null], _opts, error => {
cb(error);
});
}
cursor.explain((error, result) => {
const _opts = { error: error };
return model.hooks.execPost('aggregate', this, [result], _opts, error => {
if (error) {
Expand All @@ -732,6 +737,7 @@ Aggregate.prototype.explain = function(callback) {
return cb(null, result);
});
});
});
});
}, model.events);
};
Expand Down Expand Up @@ -977,31 +983,20 @@ Aggregate.prototype.exec = function(callback) {

const options = utils.clone(this.options || {});

const cursor = collection.aggregate(this._pipeline, options);
collection.aggregate(this._pipeline, options, (err, cursor) => {
if (err != null) {
return cb(err);
}

if (cursor != null && typeof cursor.then === 'function') {
return cursor.then(cursor => {
cursor.toArray((error, result) => {
const _opts = { error: error };
model.hooks.execPost('aggregate', this, [result], _opts, (error, result) => {
if (error) {
return cb(error);
}
cursor.toArray((error, result) => {
const _opts = { error: error };
model.hooks.execPost('aggregate', this, [result], _opts, (error, result) => {
if (error) {
return cb(error);
}

cb(null, result);
});
cb(null, result);
});
}).catch(err => cb(err));
}

cursor.toArray((error, result) => {
const _opts = { error: error };
model.hooks.execPost('aggregate', this, [result], _opts, (error, result) => {
if (error) {
return cb(error);
}

cb(null, result);
});
});
});
Expand Down
2 changes: 1 addition & 1 deletion lib/drivers/node-mongodb-native/collection.js
Expand Up @@ -66,7 +66,7 @@ NativeCollection.prototype.onClose = function(force) {
* ignore
*/

const syncCollectionMethods = { watch: true, find: true };
const syncCollectionMethods = { watch: true, find: true, aggregate: true };

/*!
* Copy the collection methods and make them subject to queues
Expand Down

0 comments on commit 4e2f7bf

Please sign in to comment.