Skip to content

Commit a9e2245

Browse files
authored
Merge pull request #13126 from lpizzinidev/gh-13125
fix(aggregate): added `await` to prevent exception in aggregate exec
2 parents 7f6295c + 361d375 commit a9e2245

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

lib/aggregate.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1049,7 +1049,7 @@ Aggregate.prototype.exec = async function exec() {
10491049
const options = clone(this.options || {});
10501050
let result;
10511051
try {
1052-
const cursor = collection.aggregate(this._pipeline, options);
1052+
const cursor = await collection.aggregate(this._pipeline, options);
10531053
result = await cursor.toArray();
10541054
} catch (error) {
10551055
await new Promise((resolve, reject) => {

test/aggregate.test.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1242,4 +1242,22 @@ describe('aggregate: ', function() {
12421242
});
12431243
});
12441244

1245+
it('should not throw error if database connection has not been established (gh-13125)', async function() {
1246+
const m = new mongoose.Mongoose();
1247+
const mySchema = new Schema({ test: String });
1248+
const M = m.model('Test', mySchema);
1249+
1250+
const aggregate = M.aggregate();
1251+
aggregate.match({ $match: { foo: 'bar' } });
1252+
1253+
const p = aggregate.exec();
1254+
1255+
await new Promise(resolve => setTimeout(resolve, 0));
1256+
await m.connect(start.uri);
1257+
1258+
assert.doesNotThrow(async function() {
1259+
await p;
1260+
});
1261+
await m.disconnect();
1262+
});
12451263
});

0 commit comments

Comments
 (0)