Automattic / mongoose

MongoDB object modeling designed to work in an asynchronous environment.
https://mongoosejs.com
MIT License
26.77k stars 3.8k forks source link

Can not creating a Schema with a field named 'parent' #14663

Open jasonhai-bee opened 3 weeks ago

jasonhai-bee commented 3 weeks ago

Prerequisites

Mongoose version

8.4.1

Node.js version

20.10.0

MongoDB server version

7.0.11

Typescript version (if applicable)

No response

Description

Can not creating a Schema with a field named 'parent'

Steps to Reproduce

(parent: { type: parent, default: {} },) const parent = mongoose.Schema( { name: { type: String }, translate: { type: Object }, uuid: { type: String }, }, { _id: false } );

Expected Behavior

No response

vkarpov15 commented 2 weeks ago

I'm unable to repro, the following script executes successfully without error. Please modify the following script to demonstrate your issue.

const mongoose = require('mongoose');

const parent = mongoose.Schema({
  name: { type: String },
  translate: { type: Object },
  uuid: { type: String },
}, { _id: false });

const schema = mongoose.Schema({
  parent
});

const TestModel = mongoose.model('Test', schema);

run().catch(err => {
  console.error(err);
  process.exit(-1);
});

async function run() {
  await mongoose.connect('mongodb://127.0.0.1:27017/mongoose_test');
  const { _id } = await TestModel.create({ parent: { name: 'test' } });
  console.log(await TestModel.findById(_id)); // Prints document with `parent` property
}
jasonhai-bee commented 2 weeks ago

Hi @vkarpov15 ,

Thank you so much for your information. Let's reproduce these steps

import mongoose from 'mongoose'; import mongoosePaginate from 'mongoose-paginate-v2'; import uniqueValidator from 'mongoose-unique-validator'; import uuidv4 from 'uuid/v4'; const Schema = mongoose.Schema;

const parent = mongoose.Schema( { name: { type: String }, translate: { type: Object }, uuid: { type: String }, } );

const schema = Schema( { uuid: { type: String }, name: { type: String }, parent: { type: parent, default: {} }, }, { collection: 'test', versionKey: false } );

schema.plugin(uniqueValidator); schema.plugin(mongoosePaginate);

schema.pre('save', function (next) { if (this.isNew) { this.uuid = uuidv4(); }

next(); });

export const TestModel = mongoose.model('Test', schema);

vkarpov15 commented 2 weeks ago

I'm still unable to repro, the following script executes without error. What is the behavior you're seeing - are you seeing some error message?

const mongoose = require('mongoose');

const parent = mongoose.Schema({
  name: { type: String },
  translate: { type: Object },
  uuid: { type: String },
});

const schema = mongoose.Schema({
  uuid: { type: String },
  name: { type: String },
  parent: { type: parent, default: {} },
}, { collection: 'test', versionKey: false });

schema.pre('save', function (next) {
  if (this.isNew) {
    this.uuid = uuidv4();
  }
  next();
});

const TestModel = mongoose.model('Test', schema);
jasonhai-bee commented 2 weeks ago

Hi @vkarpov15

I just tried to test the model & validation from my end. Here is the error message after creating test data with the Test model above. Note that when we init the model it does not throw the error message until we use it.

Error: Test validation failed: _id: this.parent is not a function at ValidationError.inspect
node_modules/mongoose/lib/error/validation.js:50:26)

errors: {
    _id: this.parent is not a function
        at validate (/node_modules/mongoose/lib/schemaType.js:1385:13)
        at /node_modules/mongoose/lib/schemaType.js:1366:11
        at processTicksAndRejections (node:internal/process/task_queues:95:5) {
      properties: [Object],
      kind: 'unique',
      path: '_id',
      value: new ObjectId('66725743c37f9c28a4cfb84f'),
      reason: TypeError: this.parent is not a function
          at /node_modules/mongoose-unique-validator/index.js:81:56
          at new Promise (<anonymous>)
          at model.<anonymous> (/node_modules/mongoose-unique-validator/index.js:54:32)
          at SchemaObjectId.SchemaType.doValidate (/node_modules/mongoose/lib/schemaType.js:1349:24)
          at /node_modules/mongoose/lib/document.js:3060:18
          at processTicksAndRejections (node:internal/process/task_queues:77:11),
      [Symbol(mongoose#validatorError)]: true
    }
  },

You can try to create a test data with the model above to see if the same issue happens on your end.

vkarpov15 commented 1 week ago

Ah ok, it looks like the error comes from mongoose-unique-validator. We'll investigate and see if we can repro.

vkarpov15 commented 1 week ago

We put in a PR to mongoose-unique-validator to fix :point_up: Currently, the only workarounds are to either rename your path named parent, or run a forked version of mongoose-unique-validator.

jasonhai-bee commented 1 week ago

Thank you so much for your quick action @vkarpov15

viveleroi commented 1 week ago

This has been fixed in mongoose-unique-validator@5.0.1 - thanks @vkarpov15