Mongoose:CastError:在路径“items”处为值“{ value: 'x' }”投射到嵌入失败

Edd*_*ang 5 mongoose mongodb node.js typescript nested-documents

更新到 Mongoose 5.11.13 后,尝试将项目添加到文档内的子对象时出现以下错误。

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
Run Code Online (Sandbox Code Playgroud)

我的主要 Schma 称为Card. 它包含一个名为的子对象/子文档Property,它看起来像这样:

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
Run Code Online (Sandbox Code Playgroud)

用于item在 a 中插入新的查询property是:

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 },
});
Run Code Online (Sandbox Code Playgroud)

我不知道为什么会发生这种情况,因为我们有类似的查询Checklist并且它有效。我在 mongo shell 中尝试了这个查询,它在那里工作。你们能帮我弄清楚我到底错过了什么吗?

哦,我也尝试查看整个TypeError: this.ownerDocument(...).isSelected is not a function部分,但没有找到任何可以帮助我的东西

Moh*_*adi 1

您不能isSelected在架构中使用作为字段名称,因为isSelected()在内部检查我们需要在猫鼬中验证哪些路径,因此将字段名称更改为isSelect或...