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:
- Verify that the necessary dependencies are installed.
The express-session and connect-mongo packages are included in these dependencies.
- 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); - 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
}); - 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
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.
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.
Understanding int/str/float TypeErrors
Python separates numeric types from strings strictly. Concatenating, comparing, and arithmetic across type boundaries requires explicit conversion.
Common triggers
- User input is always str.
input()always returns str. Wrap withint()orfloat(). - CSV cells are all str. Even numeric-looking columns are strings until converted.
- JSON numbers vs str. json.loads preserves the JSON type — but only “123” as string in the JSON becomes str in Python.
- Format string mismatch.
"%d" % "5"raises TypeError. Useint("5")first. - Compare int and str. Python 3 fails on
"1" < 2. Convert one side first.
Diagnostic pattern
# BAD — user input treated as int
age = input("Enter your age: ")
if age >= 18: # TypeError: '>=' not supported between 'str' and 'int'
print("Adult")
# GOOD — convert first, guard failure
try:
age = int(input("Enter your age: "))
except ValueError:
print("Invalid age")
age = 0
if age >= 18:
print("Adult")
Best practices
- Convert at boundaries. Convert input, config values, and API responses to the right type immediately after loading.
- Use pydantic or dataclasses. Modern data validation libraries convert and check types automatically.
- Avoid == across types. Compare like-to-like.
Official documentation
Frequently Asked Questions
What is Python TypeError and what causes it?
TypeError is raised when an operation is applied to an object of the wrong type. Common patterns: calling a non-callable object, adding incompatible types (str + int), passing the wrong number of arguments, or accessing attributes on a NoneType. Each TypeError message names the operation and expected vs actual types, the fix is almost always to convert types explicitly (int(), str()) or fix the wrong variable assignment.
How do I quickly debug a Python TypeError?
Three steps: (1) Read the full error message, it names the exact operation and types involved. (2) Print the type of every variable in that line: print(type(var1), type(var2)). (3) Check what the function expected vs what you passed. Most TypeError fixes are 1-line type casts or fixing a variable that became None unexpectedly.
Should I catch TypeError or let it propagate?
For internal code, let TypeError propagate, it’s almost always a real bug (wrong type passed). For boundary code (parsing user input, third-party API responses), catch TypeError + ValueError together: try: parsed = int(value) except (TypeError, ValueError): parsed = 0. Catching internal TypeErrors hides bugs.
How do I prevent TypeError in production?
Three patterns: (1) Use type hints (def add(a: int, b: int) -> int) and check with mypy / pyright in CI. (2) Validate inputs at boundaries (Pydantic for FastAPI, DRF serializers for Django). (3) Default values that match expected types (return 0 not None for numeric functions). Static typing catches 80% of TypeErrors before runtime.
Where can I find more TypeError fixes?
Browse the TypeError reference hub for 220+ specific TypeError fixes. For broader Python debugging, see the Python Tutorial hub. For related error types, see ValueError and AttributeError guides.
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! 😊
