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

fix(model): avoid TypeError if insertMany() fails with error that does not have writeErrors property #13579

Merged
merged 1 commit into from
Jul 6, 2023

Conversation

vkarpov15
Copy link
Collaborator

Fix #13531

Summary

This issue is a bit tricky to test, but the idea is that, in the insertMany() error handler, the error may not necessarily have a writeErrors property. I was able to repro with the following script by shutting down my MongoDB server in the 10 seconds between "Sleep" and "Executing".

'use strict';

const mongoose = require('mongoose');

run().catch(err => console.log(err));

async function run() {
  await mongoose.connect('mongodb://127.0.0.1:27017/mongoose_test', { serverSelectionTimeoutMS: 5000 });
  const Test = mongoose.model('Test', mongoose.Schema({ name: String }));
  await Test.init();

  console.log('Sleep');
  await new Promise(resolve => setTimeout(resolve, 10000));

  console.log('Executing');
  await Test.insertMany([{ name: 'foo' }]);
  console.log('Done');
}

With this change, we'll assume that all documents failed to insert if there's an error with no writeErrors property.

Examples

@vkarpov15 vkarpov15 merged commit 7ad5eef into master Jul 6, 2023
@vkarpov15 vkarpov15 added this to the 7.3.2 milestone Jul 6, 2023
@hasezoey hasezoey deleted the vkarpov15/gh-13531 branch July 6, 2023 17:05
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.

Cannot read properties of undefined (reading 'length')
2 participants