Wednesday, 15 March 2017

MongoDB: Cannot add to a Sub-document of a Sub-document

I am attempting to use Mongoose to add an entry to the questions array. However mongoose is saying the data I am accessing is undefined.

{"content":
    {"_id":"58c6f76e06e6edda1b000007",
     "name":"testCourse",
     "__v":0,
     "subjects": 
         [{"name":"testSubject",
           "_id":"58c6f85280a5d6591c000007",
           "questions":[]}
         ]
     }
}

Here is my method that creates the entry:

module.exports.makeQuestion = function(req, res){
    var courseid = req.params.courseid;
    var subjectid = req.params.subjectid;

    console.log("cou:"+ courseid + " sub:"+ subjectid);

    Course.findById(courseid, function(err, course){
      course.subjects.questions.push({question: req.params.question,
                                      answer:req.params.answer})
      sendJSONResponse(res, 200, course);
    });
}

What error am I making when adding the entry?



via Tjoudrey

No comments:

Post a Comment