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

fix(aggregate): added await to prevent exception in aggregate exec #13126

Merged
merged 5 commits into from
Mar 6, 2023

Conversation

lpizzinidev
Copy link
Contributor

@lpizzinidev lpizzinidev commented Mar 4, 2023

closes #13125

@lpizzinidev lpizzinidev changed the title fix(aggregate): added connection check on aggregate execution to prevent exception fix(aggregate): added await to prevent exception in aggregate exec Mar 4, 2023
@jrjake
Copy link

jrjake commented Mar 4, 2023

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();
Copy link
Collaborator

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.

Copy link
Collaborator

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())

Copy link
Contributor Author

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 Show resolved Hide resolved
aggregate.match({ $text: { $search: 'test' } });

assert.doesNotThrow(async function() {
await aggregate.exec();
Copy link
Collaborator

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

it('returns a promise if buffering and no callback (gh-7676)', async function() {

Copy link
Collaborator

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();
Copy link
Collaborator

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())

lib/aggregate.js Outdated Show resolved Hide resolved
@@ -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);
Copy link
Collaborator

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.

Copy link
Collaborator

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

@vkarpov15 vkarpov15 merged commit a9e2245 into Automattic:master Mar 6, 2023
vkarpov15 added a commit that referenced this pull request Mar 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

TypeError: cursor.toArray is not a function
6 participants