javascript - Cast to undefined failed for value "[object Object]" at path in node.js -


i trying push mongoose object object, in quiz -> questions(array of questions), here trying push question object questions array after saving question object. below function have written accomplish task

function addquestiontoquiz(req,res,next){ var q = new question({questionbody:req.body.questionbody,     options:[req.body.option1,req.body.option2,req.body.option3,req.body.option4],     answer:req.body.answer }); q.save(function(err,question){     if(err){         res.status(422).send("problem :"+err.message);     }else {         quiz.findoneandupdate({quizname:req.params.quizname},{$push:{questions:question}},{upsert:true},function(err){             if(err){                 res.status(422).send("problem: "+err.message);             } else             {                 res.redirect('/quiz/'+req.params.quizname+'/questions');             }         });     } }); } 

but getting following error while doing :

cast undefined failed value "[object object]" @ path "questions" 

here quiz model

var question = app.model('question'); var schema = mongoose.schema({ quizname:{type:string,unique:true,required:true}, questions:[{type:question}]  }); 

the question model :

var schema = mongoose.schema({ questionbody :{type:string,required:true}, options:[string], numofoptions:{type:number}, answer:{type:string,required:true}  }); 

i stuck @ point, please help.

the error result of incorrect schema , model definitions.

schemas should defined instances of mongoose.schema constructor. models should defined mongoose.model method should supplied schema second argument.

you can refactor question model definition in manner:

// note use of 'new mongoose.schema' var questionschema = new mongoose.schema({   questionbody: {type:string, required:true},   options: [string],   numofoptions: {type:number},   answer: {type:string,required:true} });  // 'mongoose.model' method should given schema definition. var question = mongoose.model('question', questionschema);  module.exports = question; 

the quiz model can defined such:

var quizschema = new mongoose.schema({   quizname: {type:string,unique:true,required:true},   // quiz has many questions.   questions:[{type: mongoose.schema.types.objectid, ref: 'question}] });  var quiz = mongoose.model('quiz', quizschema);  module.exports = quiz; 

there no need use $push query operator. once quiz instance has been obtained findoneandupdate command, question instance can added directly questions array of quiz instance:

quiz.findoneandupdate({quizname:req.params.quizname}, function(err, quiz) {   if(err) {     res.status(422).send("problem: "+err.message);   } else {     quiz.questions.push(question);     res.redirect('/quiz/'+req.params.quizname+'/questions');   } }); 

Comments

Popular posts from this blog

routing - AngularJS State management ->load multiple states in one page -

python - GRASS parser() error -

Swift game error message -