Я пытался из мангуста схемы в nodejs и в конечном итоге в следующем error.I определил две мангуст схемы следующим образомCastError: Cast к undefined_method Ошибки при попытке сохранить в мангусте
var markSchema = mongoose.Schema({
examName: String,
scores : mongoose.Schema.Types.Mixed
});
var studentSchema = mongoose.Schema({
studentID: {type : String, unique : true} ,
marks: [{type : mongoose.Types.ObjectId, ref : 'marks'}]
});
mongoose.model('marks',markSchema);
mongoose.model('student',studentSchema);
И я используя его в мой маршрутизатор, как
var studentBody = {
"studentID": "ST12",
"marks": []
};
var markz = {
"examName": "Series 1",
"scores": {
"maths": {
"score": 48,
"total": 50,
"teacher": "xxxx"
}
}
};
var marks;
marks = new Marks(markz);
marks.save(function(err,mark){
if(err){
console.log("Some problem occured while writing the values to the database "+err);
}
studentBody.marks.push(mark._id);
var student = new Student(studentBody);
console.log(JSON.stringify(studentBody,null,4)); // This prints as expected with ObjectId
student.save(function(err,student){ // CastError happens here
if(err){
console.log("Problem occured while writing the values to the database"+err);
}
else{
var formatted = "Saved to database :: "+JSON.stringify(student,null,4);
console.log(formatted);
}
});
});
Но я получаю CastError и след ошибки является
CastError: Cast to undefined_method failed for value "547e8cddd90f60a210643ddb" at path "marks"
Он печатает, как и ожидалось, с Objectid в массиве, когда я регистрирую его, но он дает вышеприведенный эффект castError при попытке сохранить данные в mongoDB.
Может ли кто-нибудь помочь мне разобраться? Благодаря
Да, это работает отлично !! Спасибо за вашу помощь. – Tony