Tuesday, 14 March 2017

MongoDB: id of sub-document not found

I have created the following entry in my database:

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

I am attempting to insert a question into to the subject within the course but when sending:

POST 127.0.0.1:3000/api/course/58c6f76e06e6edda1b000007/subject/58c6f85280a5d6591c000007/question

with question: q and answer: a in the x-www-form-URLencoded body.

I receive:

{"content":{"message":"subjectid not found"}}

Why is it that my entry is not being found and added to?

Below are some relevant files:

index.js

//controllers
var ctrlStu= require('../controllers/student');
var ctrlInst= require('../controllers/instructor');
var ctrlAdmin= require('../controllers/admin');

var ctrlCour = require('../controllers/course');
var ctrlSubj = require('../controllers/subject');
var ctrlQues = require('../controllers/question');
var ctrlQuiz = require('../controllers/quiz');
//router
var express = require('express');
var router = express.Router();

//Student Methods
router.get('/student', ctrlStu.listStudents);
router.get('/student/:studentid', ctrlStu.studentIDLookup);
//TODO; proper autherntication
router.get('/student/:studentName/:studentPass', ctrlStu.studentAuth);
router.post('/student', ctrlStu.makeStudent);
router.delete('/student/:studentid', ctrlStu.deleteStudent);

//Instructor Methods
router.get('/instructor', ctrlInst.listInstructors);
router.get('/instructor/:instructorid', ctrlInst.instructorIDLookup);
//TODO; proper autherntication
router.get('/instructor/:instructorName/:instructorPass', ctrlInst.instructorAuth);
router.post('/instructor', ctrlInst.makeInstructor);
router.delete('/instructor/:instructorid', ctrlInst.deleteInstructor);

//Admin Methods
router.get('/admin', ctrlAdmin.listAdmin);
router.get('/admin/:adminid', ctrlAdmin.adminIDLookup);
//TODO; proper autherntication
router.get('/admin/:adminName/:adminPass', ctrlAdmin.adminAuth);
router.post('/admin', ctrlAdmin.makeAdmin);
router.delete('/admin/:adminid', ctrlAdmin.deleteAdmin);

//COURSE METHODS
router.post('/course', ctrlCour.makeCourse);
router.get('/course', ctrlCour.listCourses);
router.get('/course/:courseid', ctrlCour.courseIDLookup);
router.delete('/course/:courseid', ctrlCour.deleteCourse);

//SUBJECT METHODS
router.post('/course/:courseid/subject', ctrlSubj.makeSubject);

//QUESTI0N METHODS
router.post('/course/:courseid/subject/:subjectid/question', ctrlQues.makeQuestion);

//QUIZ METHODS
router.post('/course/:courseid/quiz', ctrlQuiz.makeQuiz);

module.exports = router;

question.js

var mongoose = require('mongoose');
var Course = mongoose.model('Course');
var Subject = mongoose.model('Subject');
var Question = mongoose.model('Question');

var sendJSONResponse = function(res, status, content){
   res.status(status);
   res.json({content});
};

var addQuestion = function(req, res, subject){
  if(!subject){
    sendJSONResponse(res, 404, {"message":"subjectid not found"});
    return;
  }
  else{
    subject.questions.push({question: req.body.question,
                            answer: req.body.answer,
                            falseAnswer: req.body.falseanswer});
    subject.save(function(err, course){
      var thisQuestion
      if(err){
        sendJSONResponse(res, 400, err);
      } else{
        thisQuestion = subject.questions[subject.questions.length - 1];
        sendJSONResponse(res, 201, thisQuestion);
      }
    });
  }
}

var seekSubject = function(req, res, course){
  if(req.params && req.params.subjectid){
    Subject.findById(req.params.subjectid).exec(function(err, subject){
      if(!subject){
         //sendJSONResponse(res, 404, {"message":"subjectid not found"});
         return
      }
      else if(err){
         sendJSONResponse(res, 404, err);
         return;
      }
      //sendJSONResponse(res, 200, subject);
      return subject
      });
  } else{
     sendJSONResponse(res, 404, {
        "message":"no subjectid in request"
     });
  }
};

module.exports.makeQuestion = function(req, res){
    var courseid = req.params.courseid;
    if(courseid) Course.findById(courseid).select('subjects').exec(
      function(err, course){
        if(err){
          sendJSONResponse(res, 400, err);
          return;
        }
        else var subject =  seekSubject(req, res, course);
        console.log(subject);
        addQuestion(req, res, subject);
    });
}

MODELS:

Course:

var mongoose = require('mongoose')
var subject = require('./subject.js');

var courseSchema = new mongoose.Schema({
   name : {type: String,
            unique: true,
            required: true},
   subjects : [subject.schema]
});

module.exports = mongoose.model('Course', courseSchema);

Subject:

var mongoose = require('mongoose')
var question = require('./question.js');

var subjectSchema = new mongoose.Schema({
   name : String,
   questions : [question.schema]
});

module.exports = mongoose.model('Subject', subjectSchema);

Question:

var mongoose = require('mongoose')

var questionSchema = new mongoose.Schema({
   question: {type: String, required: true},
   answer: {type: String, required: true},
   falseAnswer: [String]
});

module.exports = mongoose.model('Question', questionSchema);



via Tjoudrey

No comments:

Post a Comment