Uploaded image for project: 'Node.js Driver'
  1. Node.js Driver
  2. NODE-4879

cursor.hasNext() applies transform from cursor.map() to next document in cursor

    • Type: Icon: Bug Bug
    • Resolution: Duplicate
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 4.12.1
    • Component/s: Cursors
    • 2
    • Not Needed
    • Hide

      1. What would you like to communicate to the user about this feature?
      2. Would you like the user to see examples of the syntax and/or executable code and its output?
      3. Which versions of the driver/connector does this apply to?

      Show
      1. What would you like to communicate to the user about this feature? 2. Would you like the user to see examples of the syntax and/or executable code and its output? 3. Which versions of the driver/connector does this apply to?

      What problem are you facing?

      The cursor.hasNext() has a side-effect:
      It is applying the transform, specified in cursor.map(), to the next document in the cursor.

      When calling cursor.next() after having called cursor.hasNext(), the transform is applied again against the already transformed document. In my case, this resulted in an error.

      I expect the cursor.next() function to apply the transform, but cursor.hasNext() should leave the documents in the cursor as they are.
      I expect cursor.hasNext() to just tell me if there is a next document in the cursor, without any side-effects.

      What driver and relevant dependency versions are you using?

      NodeJS driver 4.12.1

      Steps to reproduce?

      1. create a cursor with a .map function:
      const cursor = collection.find({}).map(doc => transform(doc));
      
      1. Iterate over cursor:
      while(await cursor.hasNext()){
        const transformedDoc = await cursor.next(); // this throws an error
      }

            Assignee:
            Unassigned Unassigned
            Reporter:
            dev@patzo.org Patzo Development
            Votes:
            1 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: