【发布时间】:2018-01-18 07:02:07
【问题描述】:
我正在尝试保存具有带有对象字段的架构的猫鼬模型。当我尝试保存时,出现以下错误。我错过了什么?
我怀疑这可能与 mongoose 对象不太像标准的 javascript 对象有关,因为它们已被更改。但是,让我感到困惑的是,我在代码的另一部分中使用具有嵌套对象字段并且有效的架构做完全相同的事情。
我尝试过的:
- 为了进行测试,我将架构更改为将 resulttype 和 resultround 作为单独的字段列出,没有嵌套,并且当我这样做时它起作用了。
- 我还尝试使用我的模型之外的键值对创建一个对象,然后将该对象传递给我的模型。那也没有用。
架构
var ResultSchema = new mongoose.Schema({
event_id : mongoose.Schema.ObjectId,
event_name: String,
event_type: String,
resultdate : String,
resulttype: {
type: String,
round: Number
},
// resulttype: String,
// resultround: Number,
});
模型保存:
var newResult = new ResultModel({
// objNewResult
event_id: req.body.eventid,//hidden field
event_name: req.body.eventname, //hidden field
resultdate: req.body.resultdate,
// resulttype: resulttypelist,
// resultround: resultroundlist,
resulttype: {
type: req.body.resulttypelist,
round: req.body.resultroundlist
}
});
newResult.save(function (err, result) {
if (err) {
console.log("SOMETHING WENT WRONG");
console.log(err);
} else {
console.log("SUCCESSFUL RESULT ADDITION");
}
});
错误:
ValidationError: results validation failed: resulttype: Cast to String failed for value "{ type: 'standard', round: '1' }" at path "resulttype"
【问题讨论】: