How to Fix MongoDB Error 142 (JSUncatchableError)
You see mongodb error 142 when the database engine hits an uncatchable JavaScript error. You fix this JSUncatchableError by removing infinite loops or replacing heavy server-side scripts with native queries. MongoDB runs an internal JavaScript engine for specific query operators and custom functions. When a script breaks engine limits or gets killed, the server stops the task. The database cannot catch this fault inside the user script. Instead, it returns code 142 to the client app. What cau
Your coding agents run with your access.
Claude Code reaches prod with a developer's credentials, and approval prompts get clicked through. hoop.dev checks every query, masks sensitive data, and holds risky changes for a human.
You see mongodb error 142 when the database engine hits an uncatchable JavaScript error. You fix this JSUncatchableError by removing infinite loops or replacing heavy server-side scripts with native queries.
MongoDB runs an internal JavaScript engine for specific query operators and custom functions. When a script breaks engine limits or gets killed, the server stops the task. The database cannot catch this fault inside the user script. Instead, it returns code 142 to the client app.
What causes mongodb error 142 in your database
The official name for mongodb error 142 is JSUncatchableError. This failure happens inside the server-side JavaScript runtime. Normal script bugs throw standard errors that your code can catch. An uncatchable error means the runtime itself had to stop the script right away.
Infinite loops are a common trigger for this error code. A bad loop runs until it hits a timeout or an operator stops it. Memory exhaustion inside the script engine also triggers this failure. If a custom function builds a massive array in memory, the engine halts execution.
Stored functions can also play a part in this problem. In MongoDB, the built-in read role grants read access to all non-system collections. It also allows reading the system.js collection in its database. If a client runs a broken script from that collection, the runtime can fail.
High cluster load makes script timeouts much more likely. When CPU or disk wait times spike, slow JavaScript tasks hit their time limits fast. The database engine kills the script thread to protect the rest of the node.
How to diagnose and fix JSUncatchableError in production
Start your check by reading the database server logs around the failure time. Look for slow query warnings or killed operations that match error code 142. Identify which collection and which app user sent the failing script.
Check the script logic for unbounded loops or deep recursion. Make sure every loop has a clear exit rule. Also verify that the script does not load huge document sets into memory at once.
MongoDB Authentication & Authorization + End-to-End Encryption: architecture patterns and best practices
Continue reading? Get the full guide.
Free. No spam. Unsubscribe anytime.
The best fix is to replace server-side JavaScript with native aggregation pipelines. Native operators run in compiled code and do not use the JavaScript engine. They run much faster and will not trigger JSUncatchableError.
Do not grant broad admin rights to debug this error in production. MongoDB uses role-based rules to grant actions on resources down to the collection level. Keep engineers on a read role while they inspect collections and index stats.
The read role includes actions like find, listCollections, listIndexes, dbStats, and collStats. It does not allow insert, update, remove, or dropCollection actions. If a team member must fix a stored script, grant readWrite only on that single database.
How to prevent server-side script failures long term
Review your app code to find every place that sends JavaScript to the database. Move complex data checks into your app layer or convert them to standard queries. This change removes the main risk of hitting code 142.
Set strict query time limits on your client drivers. Clear timeouts stop bad queries before they starve the server of memory or CPU. When you test queries on live data, use data masking to rewrite emails and card numbers.
Audit your database users to ensure apps only hold the roles they need. Self-hosted deployments and MongoDB Atlas have different built-in roles. Still, both build their roles from the same set of privilege actions.
Inspect MongoDB wire traffic in flight
Built-in MongoDB roles govern collection access at setup time. They cannot score command risk per call or mask sensitive document values on the fly. By design, hoop.dev sits inline in the MongoDB protocol next to your server as a sidecar for your MongoDB. On every production command from mongosh, drivers, or AI agents, hoop.dev decides whether to pass, mask, block, or hold it for human review.
First, hoop.dev guardrails block bad deletes, drops, and write pipelines or hold them for approval. Next, hoop.dev data masking rewrites sensitive values in returned documents in memory and logs each decision. Explore the repo on GitHub.
Frequently asked questions
What does JSUncatchableError mean in MongoDB?
JSUncatchableError is the official name for error code 142 in MongoDB. It means the server-side JavaScript engine stopped a script due to a fatal fault, timeout, or kill signal.
Can a try-catch block inside my script prevent error 142?
No, a try-catch block inside the JavaScript code cannot stop this error. The database engine forces the script to stop from the outside when it hits memory or time limits.
What permissions do I need to inspect collections when debugging error 142?
You only need the built-in read role on the target database. This role lets you run find, listCollections, listIndexes, dbStats, and collStats without risking accidental data changes.
Related guides
In this post