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

When no results found, findOne now returns undefined instead of null

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Unknown Unknown
    • 4.1.1
    • Affects Version/s: 4.0.0, 4.1.0
    • Component/s: None
    • Not Needed

      I have traced a behavior change back to this PR - https://github.com/mongodb/node-mongodb-native/pull/2619. It changes the return value of findOne when no results are found. It used to be null (when using driver 3.x for example) but is now undefined. I found this when debugging broken tests that expected "not found" return value to be strictly null.

      Here is the latest code I am referring to - see https://github.com/mongodb/node-mongodb-native/blame/b67af3cd8b094218ec323b23e9950151cb91f1ef/src/operations/find_one.ts#L35. Pretty clearly, even when item is null, it will always be returned as undefined:
      // Return the item
      cursor.next((err, item) => {
      {{ if (err != null) return callback(err);}}
        callback(undefined, item || undefined);
      });

      This used to be (change made 9 months ago), which returned item directly:

      // Return the item
      {{ cursor.next((err, item) => {}}
        if (err != null) return callback(new MongoError(err));
        callback(undefined, item);
      {{ });}}
       
       
      The documentation I can find indicates null should be returned when no results are found (and there was no error). Asking if this change is intentional feels silly, because it must be. I would like to understand more about this change and see if there is any newer documentation that I am missing. Thank you to anyone who can help me here!

            Assignee:
            neal.beeken@mongodb.com Neal Beeken
            Reporter:
            nick@nickrobillard.com Nick Robillard
            Daria Pardue
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: