Skip to content

Conversation

@Vegapunk-debug
Copy link

@Vegapunk-debug Vegapunk-debug commented Nov 28, 2025

Fix: Prevent crash in Document.prototype.init() when called with null/undefined

Summary

Calling Document.prototype.init() with null or undefined currently throws a low-level
TypeError (Cannot read properties of null (reading '_id')). This PR adds an explicit
null/undefined check and throws a proper ObjectParameterError instead.

Motivation and Context

init() is part of the public API. While normally used internally, custom scripts or plugins
may pass invalid input. The new error message improves developer experience and prevents an
unexpected crash.

Reproduction

const mongoose = require('mongoose');
const doc = new mongoose.Document({}, new mongoose.Schema({ name: String }));

// Before:
// TypeError: Cannot read properties of null (reading '_id')
// doc.init(null);

// After:
// ObjectParameterError: Parameter "doc" to init() must be an object.
doc.init(null);

Testing

  • I have verified locally using the reproduction script above.
  • Also, ran npm test to ensure no regressions.

Kindly please review the pull request and merge it.

Thank you!

@Vegapunk-debug Vegapunk-debug force-pushed the fix/document-init-crash branch from ca2ea36 to 7954901 Compare November 28, 2025 23:03
@Vegapunk-debug
Copy link
Author

Closing in favor of clean PR #15812

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant