Symbol.asyncIterator for the cursor in version 3.0 alpha 11

Hello @zodern and @denyhs, as indicated here, to scroll the result of a search we used the syntax below which allows other asynchronous methods to be invoked within the loop:

for await (const doc of collection.find({})) {
   await collection2.findOneAsync(...);

This works in version 2.13.3 while alpha version 11 returns the error:

     TypeError: cursor[methodName] is not a function
      at Cursor.<computed> (packages/mongo/mongo_driver.js:879:30)
      at _asyncIterator (/Users/pbeato/Documents/ksource/meteor12.3/meteor-collection2/tests/node_modules/@babel/runtime/helpers/asyncIterator.js:7:41)
      at Hook.<anonymous> (multi.tests.js:92:13)
      at run (packages/meteortesting:mocha-core/server.js:36:29)
      at Context.wrappedFunction (packages/meteortesting:mocha-core/server.js:65:14)
      at processImmediate (node:internal/timers:476:21)
      at process.topLevelDomainCallback (node:domain:161:15)
      at process.callbackTrampoline (node:internal/async_hooks:128:24)

Before opening an issue I wanted to discuss with you to see if I had missed something.


Have you tried something like:

for await (const doc of collection.find({}).fetchAsync()) {
   await collection2.findOneAsync(...);

Because when you do const doc of collection.find({}), doc is now a cursor, not a document from collection.

Hi @denyhs,

I am not sure this: has been superseded by another concept.
Also discussed here: Migration to 2.8 Async version - #2 by grubba where I see @pbeato has beed the initiator of the conversation.

Once data is fetched, there are many ways to iterate over it. The subject of interest is how to iterate over the cursor like in the “old Meteor”.

collection.find({}) is supposed to be a cursor while doc remains a doc as before. This is what I understand from the conversations above.

Just for the sake of completeness, I quote what is stated in the migration document to 2.8 that we have applied in all our applications:

There’s also [Symbol.asyncIterator], so this code should work:
for await (const document of collection.find(query, options)) /* … */

At the time the forEachAsync function did not allow the use of an asynchronous callback, is this now possible?

Hi @denyhs and @grubba, have you figured out yet whether it will be possible to use the asyncIterator to scroll mongo cursors as in v2.13.3? In 3 alpha 15 are there any changes in this regard? We are migrating packages that provide for their use in an asynchronous context. Thank you.

1 Like

Hey @pbeato, sorry for the delay.

Yeah, it will be possible to continue using the async iterator.
The only change we have planned so far for the MongoDB package was that we added a clearer error message when using sync methods in the server(PR)

I have just made an attempted to update to beta.0 and I am still running into this issue where the AsyncIterator doesn’t exist.

Is there an issue tracking this somewhere?

I added a PR to resolve this one Add asyncIterator to AsyncCursor by ToyboxZach · Pull Request #12960 · meteor/meteor · GitHub