problem_solving

TypeError: Class constructor ObjectId cannot be invoked without ‘new’

It looks like you’re working with MongoDB and attempting to create a query using the ObjectId constructor. The error you’re encountering indicates that you’re trying to invoke the ObjectId constructor without using the new keyword.

In MongoDB, when you want to create an ObjectId instance, you need to use the new keyword. Here’s an example of how you can fix the issue:

const query = { _id: new ObjectId(id) };

Make sure that you have the ObjectId constructor properly imported from the MongoDB driver. In most cases, you would import it like this:

const { ObjectId } = require('mongodb');

Ensure that you have the necessary dependencies installed by running:

npm install mongodb

This assumes you are using Node.js and the official MongoDB Node.js driver. If you are using a different environment or library, the syntax might be slightly different.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top