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

Bug: TypeError: Cannot read property 'name' of undefined at walkUpdatePath #10512

Closed
andreialecu opened this issue Jul 29, 2021 · 0 comments
Closed
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@andreialecu
Copy link
Contributor

Do you want to request a feature or report a bug?

Bug

What is the current behavior?

I ran into a weird issue while working with .bulkWrite(). I'm using nestjs and graphql and it appears that input objects coming in have a null prototype.

Here's an example via console.log of the update I'm attempting:

[
  {
    updateOne: {
      filter: { _id: '7b88d3a7-0044-4e2d-8190-3b833cd49bd8' },
      update: {
        '$set': {
          'units.1': [Object: null prototype] {
            clubId: 'f3700334708f5865',
            eventId: '75dedfaf9f7b68d1',
            kind: 'tournamentClock',
            layout: 'layout-1'
          }
        }
      }
    }
  }
]

Notice the [Object: null prototype]. This results in this error:

TypeError: Cannot read property 'name' of undefined
    at walkUpdatePath (mongoose/lib/helpers/query/castUpdate.js:203:32)
    at castUpdate (mongoose/lib/helpers/query/castUpdate.js:95:7)
    at mongoose/lib/helpers/model/castBulkWrite.js:73:37

The problem is on this line:

if (val && val.constructor.name === 'Object') {

If the current behavior is a bug, please provide the steps to reproduce.

Repro:

const mongoose = require('mongoose');

mongoose.set('debug', true);

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

async function run() {
  await mongoose.connect('mongodb://localhost:27017/test', {
    useNewUrlParser: true,
    useUnifiedTopology: true,
  });

  await mongoose.connection.dropDatabase();

  const schema = new mongoose.Schema(
    {
      _id: String,
      someArray: [
        new mongoose.Schema({ message: String }, { discriminatorKey: 'kind' }),
      ],
    },
    { timestamps: false },
  );
  schema.path('someArray').discriminator(
    'Foo',
    new mongoose.Schema({
      element: {
        type: String,
        required: true,
      },
    }),
  );

  schema.path('someArray').discriminator(
    'Bar',
    new mongoose.Schema({
      product: {
        type: String,
        required: true,
      },
    }),
  );

  const update = {
    updateOne: {
      filter: { _id: 'test' },
      update: {
        $set: {
          'someArray.1': {
            __proto__: null,
            kind: 'Foo',
            element: 'test',
          },
        },
      },
    },
  };

  console.dir(update, { depth: 5 });

  const Model = mongoose.model('MyModel', schema);
  await Model.bulkWrite([update]);

  console.log('Done');
}

What is the expected behavior?

I can work around it in my code by doing something like { ...objWithNullPrototype } instead, but this should probably not crash mongoose.

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.

5.13.4

@IslandRhythms IslandRhythms added the confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. label Jul 29, 2021
@vkarpov15 vkarpov15 added this to the 5.13.6 milestone Jul 30, 2021
vkarpov15 added a commit that referenced this issue Aug 9, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Projects
None yet
Development

No branches or pull requests

3 participants