Skip to content

path discriminator with nested 'new Schema' will cause 'MissingSchemaError' when create data #8527

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

Closed
kpkonghk01 opened this issue Jan 22, 2020 · 0 comments
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@kpkonghk01
Copy link

Do you want to request a feature or report a bug?
Bug (not sure)

What is the current behavior?
throw "Schema hasn't been registered for model "NestedData". Use mongoose.model(name, schema)" when create entry

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

const mongoose = require('mongoose');
const { Schema, ObjectId, model } = mongoose;

mongoose.connect(
  'mongodb://localhost:27017',
  {
    keepAlive: true,
    dbName: 'localTest',
    useNewUrlParser: true,
    useFindAndModify: false,
    useCreateIndex: true,
    autoIndex: true,
  },
);
mongoose.set('debug', true);

(async () => {
  const ImageSchema = new mongoose.Schema({
    imageName: {
      type: String,
      required: true
    }
  });
  const Image = mongoose.model('Image', ImageSchema);

  const TextSchema = new mongoose.Schema({
    textName: {
      type: String,
      required: true
    }
  });
  const Text = mongoose.model('Text', TextSchema);

  const ItemSchema = new Schema({
    objectType: {
      type: String,
    },
  }, {
    discriminatorKey: 'objectType',
    _id: false,
  });

  const InternalItemSchemaGen = () => new Schema({
    data: {
      type: ObjectId,
      refPath: 'list.objectType',
    },
  }, {
    _id: false,
  });

  const ExternalItemSchemaGen = () => new Schema({
    data: {
      sourceId: {
        type: Number,
        required: true,
      },
    },
  }, {
    _id: false,
  });

  const NestedDataSchema = new Schema({
    // This Schema leads to 'MissingSchemaError'
    // if use plain Object instead of 'new Schema', it works normally
    data: new Schema({
      title: {
        type: String,
      },
      description: {
        type: String,
      },
    }),
  }, {
    _id: false,
  });

  const ExampleSchema = new Schema({
    test: {
      type: String,
    },
    list: [{
      type: ItemSchema,
      required: false
    }],
  });
  ExampleSchema.path('list').discriminator('Image', InternalItemSchemaGen());
  ExampleSchema.path('list').discriminator('Text', InternalItemSchemaGen());
  ExampleSchema.path('list').discriminator('ExternalSource', ExternalItemSchemaGen());
  ExampleSchema.path('list').discriminator('NestedData', NestedDataSchema);
  const Example = model('Example', ExampleSchema);

  const image1 = await Image.create({
    imageName: '01image',
  });
  const image2 = await Image.create({
    imageName: '02image',
  });
  const text1 = await Text.create({
    textName: '01text',
  });
  const text2 = await Text.create({
    textName: '02text',
  });

  // error occurs here
  const example = await Example.create({
    test: 'example',
    list: [
      {
        data: image1._id,
        objectType: 'Image',
      },
      {
        data: text2._id,
        objectType: 'Text',
      },
      {
        data: {
          sourceId: 123
        },
        objectType: 'ExternalSource',
      },
      {
        data: image2._id,
        objectType: 'Image',
      },
      {
        data: {
          title: "Aut porro",
          description: "Iusto cumque iste officiis.",
        },
        objectType: 'NestedData',
      },
      {
        data: text1._id,
        objectType: 'Text',
      },
    ],
  });
  const query = Example
    .findById(example._id)
    .populate({
      path: 'list.data',
    })
    .lean()
  const results = await query.exec();
  return results;
})().then(
  async result => {
    await mongoose.disconnect();
    process.exit(0);
  },
  async _ => {
    console.log(_);
    await mongoose.disconnect();
    process.exit(1);
  }
)

What is the expected behavior?
create without error no matter the nested schema is a plain Object or an instance of Schema

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

node: v10.17.0
mongoDB: 4.2.1
mongoose: 5.8.9

@vkarpov15 vkarpov15 added this to the 5.8.10 milestone Jan 24, 2020
@vkarpov15 vkarpov15 added has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. and removed has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue labels Jan 24, 2020
vkarpov15 added a commit that referenced this issue Jan 27, 2020
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

2 participants