我正在使用Nodejs Express和Mongoose(“ mongoose”:“ ^ 5.3.11”,)。我正在尝试向集合中添加一些数据。我正在获取所有正确的数据,但是当我使用$ push时出现错误“ CastError:为值强制转换为嵌入失败”,我一直在寻找不同的解决方案,但我无法弄清楚。
这是我的模式
var InstructorSchema= mongoose.Schema({
first_name:{
type: String
},
last_name:{
type:String
},
username:{
type:String
},
email:{
type:String
},
classes:[{
class_id:{type:[mongoose.Schema.Types.ObjectId]},
class_code:{type:String},
class_title:{type:String}
}]
});
var Instructor = module.exports = mongoose.model('Instructor', InstructorSchema);
我在这里从表单收集数据
info=[];
info['instructor_ID']=req.user.id;
info['instructor_username']=req.user.username;
info['class_id']=req.body.class_id;
info['class_code']=req.body.class_code;
info['class_title']=req.body.class_title;
这是我的数据
module.exports.register=function(info,callback){
instructor_ID=info['instructor_ID'];
instructor_username=info['instructor_username'];
class_id=info['class_id'];
class_code=info['class_code'];
class_title=info['class_title'];
这是功能
var query = {username: instructor_username};
Instructor.findOneAndUpdate(
query,
{
$push: {
"classes": {
class_id: class_id,
class_code: class_code,
class_title: class_title
}
}
},
{ safe: true,
upsert: true
},
callback
);
这是错误
CastError: Cast to embedded failed for value "{ class_id: \'5be353ead45965c74d9e2853 \',\n class_code: \'ART101 \',\n class_title: \'Arts Fundamentals \' }" at path "classes"
at new CastError (C:\Users\user\Documents\Web Development\Node\elearning\node_modules\mongoose\lib\error\cast.js:29:11)
at DocumentArray.cast (C:\Users\user\Documents\Web Development\Node\elearning\node_modules\mongoose\lib\schema\documentarray.js:349:19)
at DocumentArray.cast (C:\Users\user\Documents\Web Development\Node\elearning\node_modules\mongoose\lib\schema\documentarray.js:276:17)
at castUpdateVal (C:\Users\user\Documents\Web Development\Node\elearning\node_modules\mongoose\lib\helpers\query\castUpdate.js:365:24)
at walkUpdatePath (C:\Users\user\Documents\Web Development\Node\elearning\node_modules\mongoose\lib\helpers\query\castUpdate.js:165:24)
at castUpdate (C:\Users\user\Documents\Web Development\Node\elearning\node_modules\mongoose\lib\helpers\query\castUpdate.js:79:18)
at model.Query._castUpdate (C:\Users\user\Documents\Web Development\Node\elearning\node_modules\mongoose\lib\query.js:3861:10)
at castDoc (C:\Users\user\Documents\Web Development\Node\elearning\node_modules\mongoose\lib\query.js:3889:18)
at model.Query.Query._findAndModify (C:\Users\user\Documents\Web Development\Node\elearning\node_modules\mongoose\lib\query.js:3021:19)
at model.Query.Query._findOneAndUpdate (C:\Users\user\Documents\Web Development\Node\elearning\node_modules\mongoose\lib\query.js:2732:8)
at C:\Users\user\Documents\Web Development\Node\elearning\node_modules\kareem\index.js:278:20
at _next (C:\Users\user\Documents\Web Development\Node\elearning\node_modules\kareem\index.js:102:16)
at process.nextTick (C:\Users\user\Documents\Web Development\Node\elearning\node_modules\kareem\index.js:499:38)
at _combinedTickCallback (internal/process/next_tick.js:131:7)
at process._tickCallback (internal/process/next_tick.js:180:9)
答案 0 :(得分:0)
最后,我改变了
class_id:{type:[mongoose.Schema.Types.ObjectId]},
对于
class_id:{type: Schema.Types.Mixed},
现在可以使用
更新: 我的class_id最后有一个空格,我不知道为什么,但是我使用trim函数将其删除了。之后,使用
程序可以正常工作class_id:{type:[mongoose.Schema.Types.ObjectId]}