class constructor mongostore cannot be invoked without new

In this article, we will be handling the typeerror: class constructor mongostore cannot be invoked without new.

By the time you finish reading this article, you should be able to identify the cause of this error and apply the proper fixes.

Let us not prolong our introduction and proceed to understand this error.

What is typeerror: class constructor mongostore cannot be invoked without new?

The typeerror: class constructor mongostore cannot be invoked without new is an error message that appears in JavaScript.

The error mentioned above indicates that the issue is in the way we call the class constructor mongostore.

It specified that without using the keyword “new,” the constructor could not be invoked.

What are constructor functions?

Constructor functions are functions in JavaScript.

They are used to generate objects with a particular set of properties and methods.

These can’t be invoked without the keyword “new,” because this keyword is used to create a new instance of the object.

For this error to occur, you might have forgotten to add the “new” keyword when calling the constructor function for the MongoStore class.

Here is a sample code that triggers this error:

const session = require('express-session');
const MongoStore = require('connect-mongo')(session);

const store = MongoStore({
    url: 'mongodb://localhost:27017/myapp',
    ttl: 60 * 60 // session expiration time in seconds
});

app.use(session({
    secret: 'mysecret',
    resave: false,
    saveUninitialized: false,
    store: store
}));

Typeerror: class constructor mongostore cannot be invoked without new – SOLUTION

Time needed: 2 minutes

To solve this error, ensure that when you are calling the constructor function for the MongoStore class, you are using the “new” keyword.

Here is the guide you can follow:

  1. Verify that the necessary dependencies are installed.


    The express-session and connect-mongo packages are included in these dependencies.

  2. Require the express-session, then pass it to connect-mongo.


    This is to create a new instance of the MongoStore class.

    Example:

    const session = require(‘express-session’);
    const MongoStore = require(‘connect-mongo’)(session);

  3. Use the “new” keyword.


    Use this keyword to create a new instance of the MongoStore class, then pass it any required options.

    Example:

    const store = new MongoStore({
    url: ‘mongodb://localhost:27017/myapp’,
    ttl: 60 * 60
    });

  4. Use the store object.


    Configure your express-session middleware using the store object as a value of the store option.

    Example:

    app.use(session({
    secret: ‘mysecret’,
    resave: false,
    saveUninitialized: false,
    store: store
    }));

Complete code:

const session = require('express-session');
const MongoStore = require('connect-mongo')(session);

const store = new MongoStore({
  url: 'mongodb://localhost:27017/myapp',
  ttl: 60 * 60 // session expiration time in seconds
});

app.use(session({
  secret: 'mysecret',
  resave: false,
  saveUninitialized: false,
  store: store
}));

See also: Typeerror: class constructor servecommand cannot be invoked without ‘new’

FAQs

🗨 What is JavaScript?


JavaScript is a programming language that is high-level and object-oriented.

This language is used to create dynamic and interactive web pages.

Along with HTML and CSS, it is used to build the World Wide Web.

📌 Year it was first introduced: 1995

For your information, JavaScript has become one of the most popular programming languages in the world since it was first introduced.

Because of its popularity and versatility, it becomes a valuable skill for developers to learn.

🗨 What is TypeError?


Typeerror is an error in Python that arises when an operation or function is applied to a value of an improper type.

This error indicates that the data type of an object isn’t compatible with the operation or function being used.

Conclusion

In conclusion, the typeerror: class constructor mongostore cannot be invoked without new is encountered in JavaScript.

You can fix this error by making sure that you are using the “new” keyword when calling the constructor function for the MongoStore class.

By following the guide above, you will surely solve this error quickly.

That is all for this tutorial, IT source coders!

We hope you have learned a lot from this. Have fun coding!

Thank you for reading! 😊