gpt4 book ai didi

node.js - Mongoose 错误 : Cast to embedded failed for value

转载 作者:可可西里 更新时间:2023-11-01 10:41:03 26 4
gpt4 key购买 nike

我正在创建一个 mongoose Schema,但我得到了一个 MongooseError

这是我的方案:

let RestaurantSchema = new Schema({
ratings: {
type: [{
id: Number,
value: Number,
_id: false
}],
default: [
{ id: 1, value: 0 },
{ id: 2, value: 0 },
{ id: 3, value: 0 },
{ id: 4, value: 0 },
{ id: 5, value: 0 }
]
},
menu: [{
ratings: {
type: [{
id: Number,
value: Number,
_id: false
}],
default: [
{ id: 1, value: 0 },
{ id: 2, value: 0 },
{ id: 3, value: 0 },
{ id: 4, value: 0 },
{ id: 5, value: 0 }
]
}
}]
})

这是我得到的错误:

/var/app/node_modules/mongoose/lib/schema/documentarray.js:322
throw new CastError('embedded', valueInErrorMessage,
^
MongooseError: Cast to embedded failed for value "{ id: 5, value: 0 }" at path "rating"
at CastError (/var/app/node_modules/mongoose/lib/error/cast.js:26:11)
at DocumentArray.cast (/var/app/node_modules/mongoose/lib/schema/documentarray.js:322:19)
at DocumentArray.SchemaType.getDefault (/var/app/node_modules/mongoose/lib/schematype.js:616:23)
at EmbeddedDocument.Document.$__buildDoc (/var/app/node_modules/mongoose/lib/document.js:265:22)
at EmbeddedDocument.Document (/var/app/node_modules/mongoose/lib/document.js:61:20)
at EmbeddedDocument [as constructor] (/var/app/node_modules/mongoose/lib/types/embedded.js:31:12)
at new EmbeddedDocument (/var/app/node_modules/mongoose/lib/schema/documentarray.js:70:17)
at DocumentArray.SchemaArray [as constructor] (/var/app/node_modules/mongoose/lib/schema/array.js:67:21)
at new DocumentArray (/var/app/node_modules/mongoose/lib/schema/documentarray.js:31:13)
at Function.Schema.interpretAsType (/var/app/node_modules/mongoose/lib/schema.js:643:16)
at Schema.path (/var/app/node_modules/mongoose/lib/schema.js:563:29)
at Schema.add (/var/app/node_modules/mongoose/lib/schema.js:445:12)
at new Schema (/var/app/node_modules/mongoose/lib/schema.js:99:10)
at Object.<anonymous> (/var/app/models/restaurant.js:12:24)
at Module._compile (module.js:571:32)
at Object.Module._extensions..js (module.js:580:10)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Module.require (module.js:498:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (/var/app/controllers/restaurant.js:6:18)
at Module._compile (module.js:571:32)
at Object.Module._extensions..js (module.js:580:10)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Module.require (module.js:498:17)

我做错了什么?

最佳答案

不一定与上述情况有关,但出现此错误的情况(如发生在我身上的那样)是模式需要一个对象,但代码试图保存到一个数组。即,如果:

var MySubSchema = new Schema(
{
type: String,
meta: String
}
)

var MySchema = new Schema(
subSchema: MySubSchema,
otherSubSchema: OtherSubSchema
)

但是保存数据的路由/ Controller 如下:

var mySchemaObj = new MySchema(
{mySubSchema: [new MySubSchema( //<-- Shouldn't have array "[" here
{
type: "foo",
meta: "bar"
}
)]
}
)

. . .
. . .

mySchemaObj.save() . . . // <-- This will throw the "Cast to Embedded Failed" error, because MySchema expects a single MySubSchema Object, but the code above instantiates MySubSchema in an array.

关于node.js - Mongoose 错误 : Cast to embedded failed for value,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43356267/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com