Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mongoose 8.8.3 breaks on bulkSave: TypeError: Cannot set properties of undefined (setting '__subdocs') #15062

Closed
2 tasks done
O-Q opened this issue Dec 1, 2024 · 2 comments
Closed
2 tasks done
Labels
has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue
Milestone

Comments

@O-Q
Copy link

O-Q commented Dec 1, 2024

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.8.3

Node.js version

22

MongoDB server version

8.0.3

Typescript version (if applicable)

5.7.2

Description

I've got this error when I try to bulk save a list of documents:

TypeError: Cannot set properties of undefined (setting '__subdocs')
 at node_modules/.pnpm/mongoose@8.8.3/node_modules/mongoose/lib/plugins/saveSubdocs.js:31:39
node_modules/.pnpm/mongoose@8.8.3/node_modules/mongoose/lib/helpers/each.js:21:16
node_modules/.pnpm/mongoose@8.8.3/node_modules/mongoose/lib/plugins/saveSubdocs.js:27:9
node_modules/.pnpm/kareem@2.6.3/node_modules/kareem/index.js:113:15

Apparently this line was recently added to the source code.

https://github.com/Automattic/mongoose/blame/bb98dcf859b2cf0cc7da9d16d9d09afe445321f3/lib/plugins/saveSubdocs.js#L31

Steps to Reproduce


const subDocSchema = new Schema({ title: { type: String } });

const schema = new Schema({
	name: { type: String, required: true },
	subDoc: { type: subDocSchema }
});

const Task = mongoose.model("task", schema);

const task1 = new Task({ name: "Task 1" });
await task1.save();

const tasks = await Task.find();

for (const task of tasks) {
	task.set({ subDoc: { title: "test" } });
}

await Task.bulkSave(tasks);

Expected Behavior

No response

@vkarpov15 vkarpov15 added this to the 8.8.4 milestone Dec 3, 2024
@vkarpov15 vkarpov15 added the has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue label Dec 3, 2024
@vkarpov15
Copy link
Collaborator

I wasn't able to repro this issue with the following script:

'use strict';

const mongoose = require('mongoose');

const Schema = mongoose.Schema;

void async function main() {
  await mongoose.connect('mongodb://127.0.0.1:27017/mongoose_test');

  const subDocSchema = new Schema({ title: { type: String } });

  const schema = new Schema({
    name: { type: String, required: true },
    subDoc: { type: subDocSchema }
  });

  const Task = mongoose.model("task", schema);

  const task1 = new Task({ name: "Task 1" });
  await task1.save();

  const tasks = await Task.find();

  for (const task of tasks) {
    task.set({ subDoc: { title: "test" } });
  }

  await Task.bulkSave(tasks);
  console.log('Done');
}();

Not sure why you're getting this error, but it is an easy one-line fix so we'll just fix it.

@rhugie
Copy link

rhugie commented Dec 5, 2024

I also had this error after 8.8.3 so it's a thing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue
Projects
None yet
Development

No branches or pull requests

3 participants