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

Processing Maps can result in calling doValidate on undefined values + unclear error message #11501

Closed
hasezoey opened this issue Mar 8, 2022 · 2 comments
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@hasezoey
Copy link
Collaborator

hasezoey commented Mar 8, 2022

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

What is the current behavior?
Error on calling create

Error:

TypeError: Cannot read properties of undefined (reading 'validate')
    at SubdocumentPath.doValidate (/mnt/projects/nodejs/typegoose-testing/node_modules/mongoose/lib/schema/SubdocumentPath.js:247:18)
    at /mnt/projects/nodejs/typegoose-testing/node_modules/mongoose/lib/document.js:2719:18
    at processTicksAndRejections (node:internal/process/task_queues:78:11)

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

// NodeJS: 17.6.0
// MongoDB: 4.2-bionic (Docker)
import * as mongoose from 'mongoose'; // mongoose@6.2.4

const schema_resourceparameter = new mongoose.Schema(
  {
    value: {
      type: mongoose.Schema.Types.Mixed,
    },
  },
  { collection: 'ResourceParameter' }
);

const schema_resource = new mongoose.Schema({
  parameters: {
    type: Map,
    of: schema_resourceparameter,
  },
});

const schema_objectnested = schema_resource.clone();
schema_objectnested.set('collection', 'ObjectNested');

const schema_objectmain = schema_resource.clone();
schema_objectmain.set('collection', 'ObjectMain');
schema_objectmain.add({
  object_nested: {
    type: schema_objectnested,
  },
});

class ResourceParameter {
  public value?: any;
}

class Resource {
  public parameters: Map<string, ResourceParameter> = new Map();
}

class ObjectNested extends Resource {
  private dummy?: never;
}

class ObjectMain extends Resource {
  public object_nested?: ObjectNested;
}

const ObjectMainModel = mongoose.model('ObjectMain', schema_objectmain);

(async () => {
  await mongoose.connect(`mongodb://localhost:27017/`, {
    dbName: 'verifyMASTER',
  });

  const objectMain = new ObjectMain();

  const resourceParameter1 = new ResourceParameter();
  resourceParameter1.value = 'Hello I am a nested resource parameter';
  objectMain.parameters.set('param1', resourceParameter1);

  objectMain.object_nested = new ObjectNested();

  const resourceParameter2 = new ResourceParameter();
  resourceParameter2.value = 'Hello I am a nested resource parameter';
  objectMain.object_nested.parameters.set('param2', resourceParameter2);

  console.log('before create');

  await ObjectMainModel.create(objectMain); // TypeError: Cannot read properties of undefined (reading 'validate')

  await mongoose.disconnect();
})();

reproduction repository & branch: https://github.com/typegoose/typegoose-testing/tree/verify678

What is the expected behavior?
No Error and successful insertion

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


Extra information:
when adding console.log("doValidate", value, fn, scope, options); just before the error in mongoose code (meaning just before return value.validate(fn); in SubdocumentPath.prototype.doValidate)

i get the following values:

doValidate undefined [Function (anonymous)] {
  parameters: Map(1) {
    'param1' => {
      value: 'Hello I am a nested resource parameter',
      _id: new ObjectId("6227303b2a04a004bc9592d1")
    }
  },
  _id: new ObjectId("6227303b2a04a004bc9592d0"),
  object_nested: {
    parameters: Map(1) { 'param2' => [Object] },
    _id: new ObjectId("6227303b2a04a004bc9592d2")
  }
} {
  skipSchemaValidators: true,
  path: 'parameters.param2',
  validateModifiedOnly: undefined
}

Original issue was a typegoose issue: typegoose/typegoose#678

@IslandRhythms IslandRhythms added the typescript Types or Types-test related issue / Pull Request label Mar 9, 2022
@IslandRhythms
Copy link
Collaborator

Does not error on previous version of mongoose 6.0.x

@vkarpov15 vkarpov15 added this to the 6.2.8 milestone Mar 14, 2022
@vkarpov15 vkarpov15 added confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. and removed typescript Types or Types-test related issue / Pull Request labels Mar 19, 2022
@vkarpov15
Copy link
Collaborator

Thanks for the detailed repro script. Fix will be in 6.2.8 👍

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