5

更新到 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

我的主要施马被称为Card。它包含一个名为的子对象/子文档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 },
});

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

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

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

哦,我也试着调查整个TypeError: this.ownerDocument(...).isSelected is not a function部分,没有任何运气找到任何可以帮助我的情况

4

1 回答 1

1

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

于 2021-02-01T12:42:43.393 回答