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

CastError: Cast to embedded failed for value "{ value: 'x' }" at path "items" #9873

Closed
adnanchang opened this issue Jan 28, 2021 · 1 comment · Fixed by #9886
Closed

CastError: Cast to embedded failed for value "{ value: 'x' }" at path "items" #9873

adnanchang opened this issue Jan 28, 2021 · 1 comment · Fixed by #9886
Assignees
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. needs repro script Maybe a bug, but no repro script. The issue reporter should create a script that demos the issue
Milestone

Comments

@adnanchang
Copy link

adnanchang commented Jan 28, 2021

Hey guys,

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

@IslandRhythms IslandRhythms added the needs repro script Maybe a bug, but no repro script. The issue reporter should create a script that demos the issue label Jan 28, 2021
@vkarpov15 vkarpov15 added this to the 5.11.15 milestone Jan 29, 2021
@IslandRhythms IslandRhythms added the confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. label Feb 1, 2021
@IslandRhythms
Copy link
Collaborator

'use strict';

const mongoose = require('mongoose');

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

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: mongoose.ObjectId,
    ref: 'Box',
  },
}, {
  timestamps: { createdAt: true, updatedAt: true },
});

const Card = mongoose.model('Card', CardSchema);

const entry = new Card({
  title: 'Test', $push: {checklists:'hello', properties: 'there'}
});

async function test(entry){
await Card.findOneAndUpdate(
  {
    _id: entry._id,
    'properties._id': entry._id,
  },
  {
    $push: {
      'properties.$.items': { value: 'newItem'.trim() },
    },
  },
  {
    new: true,
  },
).then(data => {
  console.log(data);
}
).catch(err => {
  console.log(err);
});
};
test(entry);

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. needs repro script Maybe a bug, but no repro script. The issue reporter should create a script that demos the issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants