-
-
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
fix(aggregate): added await
to prevent exception in aggregate exec
#13126
Conversation
…ent exception closes Automattic#13125
await
to prevent exception in aggregate exec
I was able to test this fix in my project and confirmed that I no longer get a TypeError, just the expected timeout. Thanks for the good work! |
lib/aggregate.js
Outdated
@@ -1050,7 +1050,7 @@ Aggregate.prototype.exec = async function exec() { | |||
let result; | |||
try { | |||
const cursor = collection.aggregate(this._pipeline, options); | |||
result = await cursor.toArray(); | |||
result = await (await cursor).toArray(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually very unsexy. But it is what it is.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd prefer await cursorPromise.then(cursor => cursor.toArray())
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@vkarpov15
If collection.aggregate
resolves .then
will throw an exception.
What about?
const cursor = collection.aggregate(this._pipeline, options);
const cursorPromise = await cursor;
result = await cursorPromise.toArray();
test/aggregate.test.js
Outdated
aggregate.match({ $text: { $search: 'test' } }); | ||
|
||
assert.doesNotThrow(async function() { | ||
await aggregate.exec(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suspect this will cause some issues because this operation will eventually time out because of buffering. Probably best to save the promise and then await on it later, for example const promise = aggregate.exec(); await m.connect(start.uri); const res = await promise;
similar to
mongoose/test/collection.test.js
Line 37 in c4fd23d
it('returns a promise if buffering and no callback (gh-7676)', async function() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
or like in other parts of the tests, use await assert.rejects
lib/aggregate.js
Outdated
@@ -1050,7 +1050,7 @@ Aggregate.prototype.exec = async function exec() { | |||
let result; | |||
try { | |||
const cursor = collection.aggregate(this._pipeline, options); | |||
result = await cursor.toArray(); | |||
result = await (await cursor).toArray(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd prefer await cursorPromise.then(cursor => cursor.toArray())
Co-authored-by: Hafez <a.hafez852@gmail.com>
@@ -1049,7 +1049,7 @@ Aggregate.prototype.exec = async function exec() { | |||
const options = clone(this.options || {}); | |||
let result; | |||
try { | |||
const cursor = collection.aggregate(this._pipeline, options); | |||
const cursor = await collection.aggregate(this._pipeline, options); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like this still throws a MongoNotConnectedError: Client must be connected before running operations
. I'll merge this PR into master so I can more easily debug.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why not change the doesNotThrow
to a rejects
like in other parts of the tests? await assert.rejects
closes #13125