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

Document `ObjectId` can only be constructed with `new`

    • Type: Icon: Improvement Improvement
    • Resolution: Fixed
    • Priority: Icon: Minor - P4 Minor - P4
    • 5.2.0
    • Affects Version/s: 5.0.0
    • Component/s: None
    • Labels:
    • 1
    • Not Needed
    • 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?

      When upgrading to the MongoDB Node 5.0 driver, all calls to `ObjectId()` without `new` result in the following error:

      ```

      Class constructor ObjectId cannot be invoked without 'new'

      ```

      I couldn't find this breaking change documented anywhere, so I wasn't sure if it was actually intentional. I don't know if this was formally supported, but it's relatively common use. For example, this StackOverflow answer discusses them being equivalent and the console docs provide examples of constructing them without the keyword (I recognize that parity with the console is not guaranteed, but my understanding is that it is generally aimed for). Here's an example case of someone running into this in the community.

      If this is actually intended and I missed the change notice this could be closed. If it was intended but was not documented perhaps it could be mentioned in the upgrade guide. If it was not intended then this can serve as a bug report.

      Thanks!

      What driver and relevant dependency versions are you using?

      mongodb 5.0.1 driver

      Steps to reproduce?

      ```

      import { ObjectId } from 'mongodb'

      const x = ObjectId()

      ```

            Assignee:
            neal.beeken@mongodb.com Neal Beeken
            Reporter:
            chris.james.foster@gmail.com Christopher Foster
            Daria Pardue
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: