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

Error with Virtual Population of Field on Embedded Discriminator #6273

Closed
jimmytsao opened this issue Mar 24, 2018 · 1 comment
Closed

Error with Virtual Population of Field on Embedded Discriminator #6273

jimmytsao opened this issue Mar 24, 2018 · 1 comment
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@jimmytsao
Copy link

jimmytsao commented Mar 24, 2018

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

What is the current behavior?
When trying to perform a virtual population of a field on an embedded discriminator, I receive the following error:

VM182 utils.js:214 Uncaught TypeError: Cannot read property 'length' of undefined
    at getModelsMapForPopulate (/Users/Documents/Code/mongoose-test/node_modules/mongoose/lib/model.js:3462:24)
    at populate (/Users/Documents/Code/mongoose-test/node_modules/mongoose/lib/model.js:3039:15)
    at _populate (/Users/Documents/Code/mongoose-test/node_modules/mongoose/lib/model.js:3007:5)
    at utils.promiseOrCallback.cb (/Users/Documents/Code/mongoose-test/node_modules/mongoose/lib/model.js:2980:5)
    at Object.promiseOrCallback (/Users/Documents/Code/mongoose-test/node_modules/mongoose/lib/utils.js:211:14)
    at Function.Model.populate (/Users/Documents/Code/mongoose-test/node_modules/mongoose/lib/model.js:2979:16)
    at cb (/Users/Documents/Code/mongoose-test/node_modules/mongoose/lib/query.js:1325:17)
    at result (/Users/Documents/Code/mongoose-test/node_modules/mongodb/lib/utils.js:413:17)
    at executeCallback (/Users/Documents/Code/mongoose-test/node_modules/mongodb/lib/utils.js:405:9)
    at handleCallback (/Users/Documents/Code/mongoose-test/node_modules/mongodb/lib/utils.js:128:55)
    at self.close (/Users/Documents/Code/mongoose-test/node_modules/mongodb/lib/cursor.js:934:60)
    at handleCallback (/Users/Documents/Code/mongoose-test/node_modules/mongodb/lib/utils.js:128:55)
    at completeClose (/Users/Documents/Code/mongoose-test/node_modules/mongodb/lib/cursor.js:1073:14)
    at Cursor.close (/Users/Documents/Code/mongoose-test/node_modules/mongodb/lib/cursor.js:1086:10)
    at /Users/Documents/Code/mongoose-test/node_modules/mongodb/lib/cursor.js:934:21
    at handleCallback (/Users/Documents/Code/mongoose-test/node_modules/mongodb-core/lib/cursor.js:178:5)

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

const mongoose = require('mongoose')
mongoose.connect('mongodb://localhost/mongoosetest')
mongoose.connection.once('open', () => {

  const Schema = mongoose.Schema

  // Generate Users Model
  const userSchema = new Schema({ employeeId: Number, name: String })
  const UserModel = mongoose.model('Users', userSchema, 'users')

  // Generate Embedded Discriminators
  const eventSchema = new Schema(
    { message: String },
    { discriminatorKey: 'kind'}
  );

  const batchSchema = new Schema({ events: [eventSchema] });
  const docArray = batchSchema.path('events');

  // First embedded discriminator schema
  const clickedSchema = new Schema(
    {
      element: { type: String },
      users: [ Number ]
    },
    {
      toJSON: { virtuals: true},
      toObject: { virtuals: true}
    }
  );

  // Add virtual to first embedded discriminator schema for virtual population
  clickedSchema.virtual('users_$', {
    ref: 'Users',
    localField: 'users',
    foreignField: 'employeeId'
  })
  
  const Clicked = docArray.discriminator('Clicked', clickedSchema);

  // Second embedded discriminator
  const Purchased = docArray.discriminator('Purchased', new Schema({
    product: { type: String }
  }));

  const Batch = mongoose.model('EventBatch', batchSchema);

  // Generate Items
  const user = { employeeId: 1, name: 'Test name' }
  const batch = {
    events: [
      { kind: 'Clicked', element: '#hero', message: 'hello', users: [1] },
      { kind: 'Purchased', product: 'action-figure-1', message: 'world' }
    ]
  };

  Promise.all([UserModel.create(user), Batch.create(batch)])
    .then(function(){
      Batch.find({})
        // Populate virtual field of embedded discriminator
        .populate('events.users_$')
        .lean()
        .then(results => console.log(results) )
    })
})

What is the expected behavior?
The query should not have errored and should have populated the virtual field.

Please mention your node.js, mongoose and MongoDB version.
node: v9.7.1, mongoose: 5.0.11, MongoDB: v3.2.5

@sobafuchs
Copy link
Contributor

youre right, this looks like a confirmed bug, thank you for the full repro script

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