Skip to content

Nested schema in array causes subdocParent error when multiple Mongoose libraries are present #15466

Closed
@bootylactin

Description

@bootylactin

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Mongoose version

^8.13.2

Node.js version

20.15.0

MongoDB server version

6.0.23

Typescript version (if applicable)

No response

Description

When nesting an array of schemas inside another, Mongoose throws an error when attempting to validate. A single, nested schema (not in an array) does not throw the error. The error is:

\mongoose-issues\mongoose-shared-library\node_modules\mongoose\lib\document.js:2764
              !subdocParent.$__.activePaths.getStatePaths('modify').hasOwnProperty(fullPathToSubdoc) &&
                            ^

TypeError: Cannot read properties of undefined (reading '$__')
    at _getPathsToValidate (\mongoose-issues\mongoose-shared-library\node_modules\mongoose\lib\document.js:2764:29)
    at Document.$__validate (\mongoose-issues\mongoose-shared-library\node_modules\mongoose\lib\document.js:3013:25)
    at \mongoose-issues\nested-schema-test\node_modules\kareem\index.js:497:25

The following problem manifests at 8.13.2 and every version I tested afterward. This example code runs fine at 8.13.1 and below.

This problem is specific to how our project is structured, as we are deploying lambda functions to AWS which all use a shared db-services module which are included via the "file: " syntax in the package.json. To get this error to throw, both package.json files will need to be installed. Note that this error will not throw when only one instance of the Mongoose library is present. It's very specific to how we are using Mongoose inside our lambda functions to define schemas, but using a separate shared DB library to connect to our Mongo DBs. It requires Mongoose be installed in two separate places.

I've attempted to create the simplest example that I can which follows our project structure. The folder structure is as follows:

└── mongoose-issues/
    ├── mongoose-shared-library/
    │   ├── modules/
    │   │   └── db-service.js
    │   ├── index.js
    │   └── package.json   
    └── nested-schema-test/
        ├── package.json
        └── run-this-file.js
// run-this-file.js

const { Schema } = require( "mongoose" );
const dbService  = require( "mongoose-shared-library" ).dbService;

const nestedSchema = new Schema(
	{ name : String },
	{ _id : false }
);

const schema = new Schema( {
	subDoc   : nestedSchema,
	docArray : [ nestedSchema ]  // this array of nestedSchema causes the error
} );

const document = {
	subDoc   : { name : "Alpha" },
	docArray : [
		{ name : "Bravo" },
		{ name : "Charlie" }
	]
}

dbService( ( error, db ) => {
	if ( error ) {
		console.error( error );
	}

	const Test = new (db.model( 'Test', schema ))( document );

	Test.validate()
		.then( response => {
			console.log( 'Validation successful' )
			Test.save()
				.then( result => {
					console.log( 'Save successful' )
					console.log( result )
				} )
		} )

} )

Steps to Reproduce

mongoose-issues.zip

Unzip the attachment, and run an npm install on the package.json files inside the following:

/mongoose-issues/mongoose-shared-library/package.json
/mongoose-issues/nested-schema-test/package.json

Run the following file with Node:

/mongoose-issues/nested-schema-test/run-this-file.js

Expected Behavior

The document should validate and also save, producing the following output:

Connecting to database...
Database connected successfully.
Validation successful
Save successful
{
  subDoc: { name: 'Alpha' },
  docArray: [ { name: 'Bravo' }, { name: 'Charlie' } ],
  _id: new ObjectId('6841dbe2dfdfe56ec45c4822'),
  __v: 0
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    confirmed-bugWe've confirmed this is a bug in Mongoose and will fix it.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions