Skip to content

Cannot use isSelected with embedded schema #9884

@josephstgh

Description

@josephstgh

Hi,

I'm trying to define a schema with isSelected and embedded field but if I use both together, it will throw an exception saying doc.isSelected is not a function

const mongoose = require('mongoose');

mongoose.set('useFindAndModify', false);

const { Schema } = mongoose;

run().catch(err => console.log(err));

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

  await mongoose.connection.dropDatabase();

  const emb = new Schema({
    eType: {
      type: String,
      required: true,
      uppercase: true,
    },
    eOrigin: {
      type: String,
      required: true,
    },
    eIds: [
      {
        type: String,
      },
    ],
  }, { _id: false });

  const schema = new Schema({
    name: String,
    description: String,
    isSelected: {
      type: Boolean,
      default: false,
    },
    emb: {
      type: [emb],
      default: undefined,
      required: true,
    }
  })

  const newDoc = {
      name: 'name',
      description: 'new desc',
      isSelected: true,
      emb: [
        {
          eType: 'X',
          eOrigin: 'Y',
          eIds: ['Y', 'Z']
        }
      ]
  }

  const Model = mongoose.model('Test', schema);
  await Model.create(newDoc);

  console.log('Done', await Model.findOne(), new Date());
}
C:\mongoose-9884\node_modules\mongoose\lib\document.js:2263
    if (!doc.isSelected(path) && !doc.isModified(path)) {
             ^

TypeError: doc.isSelected is not a function
    at C:\mongoose-9884\node_modules\mongoose\lib\document.js:2263:14
    at Array.filter (<anonymous>)
    at _getPathsToValidate (C:\mongoose-9884\node_modules\mongoose\lib\document.js:2262:71)
    at model.Document.$__validate (C:\mongoose-9884\node_modules\mongoose\lib\document.js:2433:23)
    at C:\mongoose-9884\node_modules\kareem\index.js:370:33
    at processTicksAndRejections (internal/process/task_queues.js:75:11)

It works as long that I don't define the embedded schema, or I change from isSelected to something else

I'm using mongoose 5.11.13, mongodb 4.2.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    confirmed-bugWe've confirmed this is a bug in Mongoose and will fix it.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions