问题描述
更新到 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
我的主要 Schma 称为 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,items: [{
name: String,select: Boolean,}],// Properties in a card
properties: [{
name: {
type: String,items: [{
value: { type: String,default: '' },isSelected: { type: Boolean,default: false },Box: {
type: ObjectId,ref: 'Box',{
timestamps: { createdAt: true,updatedAt: true },});
用于在 item
中插入新 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
部分,但没有找到任何可以帮助我的东西
解决方法
您不能在 Schema 中使用 isSelected
作为字段名称,因为 isSelected()
在内部用于检查我们需要在 mongoose 中验证哪些路径,因此更改归档
命名为 isSelect
或 ...