Skip to content

TypeError: Cannot read properties of undefined (reading '$__') when creating document with array of objects #15499

Open
@LimChorngUan

Description

@LimChorngUan

Prerequisites

  • I have written a descriptive issue title

Mongoose version

8.15.1

Node.js version

22.14.0

MongoDB version

6.16.0

Operating system

macOS

Operating system version (i.e. 20.04, 11.3, 10)

15.4.1

Issue

Calling .create() on a schema with a simple array of objects throws
TypeError: Cannot read properties of undefined (reading '$__')
in the latest mongoose.

Minimal Repro Code in test file
Jest version: ^29.7.0

import mongoose from "mongoose";

const TestSchema = new mongoose.Schema({
  name: String,
  array: [
    {
      value: Number,
    },
  ],
});

const TestModel = mongoose.model("TestModel", TestSchema);

test("test mongoose schema creation", async () => {
  await TestModel.create([
    {
      name: "Doc1",
      array: [
        {
          value: 123,
        },
      ],
    },
  ]);

  console.log("Create succeeded!");

  expect(true).toBe(true);
});

Error:

TypeError: Cannot read properties of undefined (reading '$__')
    at _getPathsToValidate ...

Notes:

  • Works on mongoose version 8.13.0, fails on 8.14.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    interop issueissue with interop between Mongoose and another npm module that is not a Mongoose prod dependency

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions