6

After updating to Mongoose 5.11.13 I am getting the following error when trying to add an item to a sub-object inside a document.

CastError: Cast to embedded failed for value "{ value: 'new item' }" at path "items"
    at model.Query.exec (D:\repos\pushbox\node_modules\mongoose\lib\query.js:4358:21)
    at model.Query.Query.then (D:\repos\pushbox\node_modules\mongoose\lib\query.js:4452:15)
    at processTicksAndRejections (internal/process/task_queues.js:97:5) {
  messageFormat: undefined,
  stringValue: `"{ value: 'new item' }"`,
  kind: 'embedded',
  value: "{ value: 'new item' }",
  path: 'items',
  reason: TypeError: this.ownerDocument(...).isSelected is not a function

My main Schma is called Card. It holds a sub-object/sub-document called Property and it looks like this:

export const CardSchema = new mongoose.Schema({
  title: {
    type: String,
    required: true,
  },

  description: {
    type: String,
    default: '',
  },

  // Checklists in a Card
  checklists: [{
    title: {
      type: String,
      required: true,
    },
    items: [{
      name: String,
      select: Boolean,
    }],
  }],
 // Properties in a card
  properties: [{
    name: {
      type: String,
      required: true,
    },
    items: [{
      value: { type: String, default: '' },
      isSelected: { type: Boolean, default: false },
    }],
  }],

  box: {
    type: ObjectId,
    ref: 'Box',
  },
}, {
  timestamps: { createdAt: true, updatedAt: true },
});

The query being used to insert a new item inside a property is:

const newPropItem = await Card.findOneAndUpdate(
        {
          _id: cardId,
          'properties._id': propertyId,
        },
        {
          $push: {
            'properties.$.items': { value: newItem.trim() },
          },
        },
        {
          new: true,
        },
      );

I have no idea why this is happening as we have a similar query for Checklist and it works. I tried this query inside the mongo shell and it worked there. Could you guys help me figure out what exactly am I missing?

Oh and I tried looking into the whole TypeError: this.ownerDocument(...).isSelected is not a function part as well, didnt have any luck finding anything that could help me in my case

Edd Chang
  • 931
  • 1
  • 7
  • 16

1 Answers1

1

you can not using isSelected in Schema as a field name because isSelected() internally for checking which paths we need to validate in mongoose,so change filed name to isSelect or ...

Mohammad Yaser Ahmadi
  • 4,664
  • 3
  • 17
  • 39
  • You are right. As you can see in the Schema I also have `Checklists` which uses the same structure but uses the variable `select`. Thats why `Checklists` kept working and `Properties` started to fail after the update to `5.11.13`. This is now recognized as an official bug by the Mongoose community and they're releasing the fix in `5.11.15`. Thank you so much! You can follow the bug here: https://github.com/Automattic/mongoose/pull/9886 – Edd Chang Feb 02 '21 at 14:28