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 use isSelected with embedded schema #9884

Closed
josephstgh opened this issue Feb 1, 2021 · 0 comments · Fixed by #9886
Closed

Cannot use isSelected with embedded schema #9884

josephstgh opened this issue Feb 1, 2021 · 0 comments · Fixed by #9886
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.

Comments

@josephstgh
Copy link

josephstgh commented Feb 1, 2021

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

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

Successfully merging a pull request may close this issue.

2 participants