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

Cannot read property 'schema' of undefined for discriminated nested array when using refPath #8837

Closed
qqilihq opened this issue Apr 22, 2020 · 2 comments · Fixed by #8878
Closed
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@qqilihq
Copy link

qqilihq commented Apr 22, 2020

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

Bug

What is the current behavior?

We have a schema with (1) nested documents, which (2) use discriminators, and where (3) (only) some of the discriminated types uses a refPath. With 5.9.9 and 5.9.10 we get an error Cannot read property 'schema' of undefined when we try to populate this reference.

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

import mongoose from 'mongoose';

beforeAll(async () => mongoose.connect(process.env.MONGO_URL as string));

afterAll(async () => mongoose.disconnect());

it('Mongoose getModelsMapForPopulate issue', async () => {
  const nested = new mongoose.Schema(
    {
      // nothing here
    },
    {
      discriminatorKey: 'type'
    }
  );

  const main = new mongoose.Schema({
    items: [nested]
  });

  const nestedDiscriminated = new mongoose.Schema({
    refPath: { type: String, required: true },
    ref: { type: mongoose.Types.ObjectId, refPath: 'items.refPath' }
    // just for illustration; this works:
    // ref: { type: mongoose.Types.ObjectId, ref: 'getModelsMapForPopulateIssue_other' }
  });

  const itemsType = main.path('items') as mongoose.Schema.Types.DocumentArray;
  itemsType.discriminator('discriminated', nestedDiscriminated);

  const MainModel = mongoose.model<any>('getModelsMapForPopulateIssue_main', main);

  const OtherModel = mongoose.model<any>(
    'getModelsMapForPopulateIssue_other',
    new mongoose.Schema({
      name: { type: String }
    })
  );

  const otherDoc = new OtherModel({ name: 'hello world' });
  await otherDoc.save();

  await new MainModel({
    items: [
      {
        type: 'not_discriminated'
      },
      {
        type: 'discriminated',
        refPath: 'getModelsMapForPopulateIssue_other',
        ref: otherDoc._id
      }
    ]
  }).save();

  const result = await MainModel.find({}).populate('items.ref').exec();
  expect(result[0].items[1].ref.name).toEqual('hello world');
});

This might be a regression related to this?

#8731

What is the expected behavior?

The test should pass, and the document should be populated. Instead we’re now receiving the following error:

    TypeError: Cannot read property 'schema' of undefined

      at getModelsMapForPopulate (node_modules/mongoose/lib/helpers/populate/getModelsMapForPopulate.js:232:94)
      at populate (node_modules/mongoose/lib/model.js:4313:21)
      at _populate (node_modules/mongoose/lib/model.js:4283:5)
      at node_modules/mongoose/lib/model.js:4258:5
      at promiseOrCallback (node_modules/mongoose/lib/helpers/promiseOrCallback.js:9:12)
      at Function.Model.populate (node_modules/mongoose/lib/model.js:4256:10)
      at cb (node_modules/mongoose/lib/query.js:1934:17)
      at node_modules/mongodb/lib/utils.js:731:5
      at handleCallback (node_modules/mongodb/lib/utils.js:128:55)
      at node_modules/mongodb/lib/cursor.js:841:66
      at handleCallback (node_modules/mongodb/lib/utils.js:128:55)
      at completeClose (node_modules/mongodb/lib/cursor.js:929:16)
      at Cursor.close (node_modules/mongodb/lib/cursor.js:948:12)
      at node_modules/mongodb/lib/cursor.js:841:27
      at handleCallback (node_modules/mongodb/lib/core/cursor.js:32:5)
      at node_modules/mongodb/lib/core/cursor.js:685:38

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

  • Node.js v13.8.0
  • Mongoose 5.9.10
  • MongoDB 3.6.7
@AbdelrahmanHafez AbdelrahmanHafez added the has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue label Apr 22, 2020
@AbdelrahmanHafez
Copy link
Collaborator

AbdelrahmanHafez commented Apr 26, 2020

Playing around with this, I can confirm that this error appears, slightly modified the reproduction script:

'use strict';
const mongoose = require('./');
const { Schema } = mongoose;
const assert = require('assert');


run().catch(console.error);

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

  await mongoose.connection.dropDatabase();


  const nestedSchema = new Schema({}, { discriminatorKey: 'type' });
  const mainSchema = new Schema({ items: [nestedSchema] });

  const nestedDiscriminated = new Schema({
    modelName: { type: String, required: true },
    otherId: { type: mongoose.Types.ObjectId, refPath: 'items.modelName' }
  });

  const itemsType = mainSchema.path('items');
  itemsType.discriminator('discriminated', nestedDiscriminated);

  const Main = mongoose.model('Main', mainSchema);
  const Other = mongoose.model('Other', new Schema({ message: String }));

  const other = await Other.create({ message: 'hello world' });

  await Main.create({
    items: [
      { },
      {
        type: 'discriminated',
        modelName: 'Other',
        otherId: other._id
      }
    ]
  });

  const result = await Main.findOne().populate('items.otherId');
  assert.equal(result.items[1].otherId.message, 'hello world');
  console.log('Done.');
}

@AbdelrahmanHafez AbdelrahmanHafez added 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 Apr 26, 2020
@AbdelrahmanHafez
Copy link
Collaborator

A bit nicer script as I work to understand the issue more:

'use strict';
const mongoose = require('./');
const { Schema } = mongoose;
const assert = require('assert');


run().catch(console.error);

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

  await mongoose.connection.dropDatabase();


  const eventSchema = new Schema({}, { discriminatorKey: 'type' });
  const eventsListSchema = new Schema({ events: [eventSchema] });

  const clickEventSchema = new Schema({
    modelName: { type: String, required: true },
    productId: { type: Schema.ObjectId, refPath: 'events.modelName' }
  });

  const eventsSchemaType = eventsListSchema.path('events');
  eventsSchemaType.discriminator('ClickEvent', clickEventSchema);

  const EventsList = mongoose.model('EventsList', eventsListSchema);
  const Product = mongoose.model('Product', new Schema({ name: String }));

  const product = await Product.create({ name: 'GTX 1050 Ti' });

  await EventsList.create({
    events: [
      { },
      {
        type: 'ClickEvent',
        modelName: 'Product',
        productId: product._id
      }
    ]
  });

  const result = await EventsList.findOne().populate('events.productId');
  assert.equal(result.events[1].productId.name, 'GTX 1050 Ti');
}

@AbdelrahmanHafez AbdelrahmanHafez added this to the 5.9.12 milestone Apr 27, 2020
AbdelrahmanHafez added a commit to AbdelrahmanHafez/mongoose that referenced this issue Apr 27, 2020
AbdelrahmanHafez added a commit to AbdelrahmanHafez/mongoose that referenced this issue Apr 27, 2020
@AbdelrahmanHafez AbdelrahmanHafez modified the milestones: 5.9.12, 5.9.11 Apr 27, 2020
@vkarpov15 vkarpov15 modified the milestones: 5.9.11, 5.9.12 Apr 30, 2020
vkarpov15 added a commit that referenced this issue Apr 30, 2020
check discriminator existence before accessing schema in getModelsMapForPopulate
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
3 participants