I am trying to create a sub-document with my express API through postman with the following request:
POST /api/course/58c6f76e06e6edda1b000007/subject/58c6f85280a5d6591c000007/question
I am also sending x-www-forum-urlencoded data with names question and answer.
Here is the error that is output in the terminal:
Error: Can't set headers after they are sent.
at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:335:11)
at ServerResponse.header (/home/tyler/Dropbox/Projects/Curricula/node_modules/express/lib/response.js:719:10)
at ServerResponse.send (/home/tyler/Dropbox/Projects/Curricula/node_modules/express/lib/response.js:164:12)
at ServerResponse.json (/home/tyler/Dropbox/Projects/Curricula/node_modules/express/lib/response.js:250:15)
at sendJSONResponse (/home/tyler/Dropbox/Projects/Curricula/API/controllers/question.js:8:8)
at Promise.<anonymous> (/home/tyler/Dropbox/Projects/Curricula/API/controllers/question.js:36:10)
at Promise.<anonymous> (/home/tyler/Dropbox/Projects/Curricula/node_modules/mpromise/lib/promise.js:171:8)
at emitOne (events.js:77:13)
at Promise.emit (events.js:169:7)
at Promise.emit (/home/tyler/Dropbox/Projects/Curricula/node_modules/mpromise/lib/promise.js:88:38)
Here is my controller file and relevant data models:
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"});
}
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);
addQuestion(req, res, subject);
});
}
Course Model
var mongoose = require('mongoose')
var subject = require('./subject.js');
var courseSchema = new mongoose.Schema({
name : {type: String,
unique: true,
required: true},
subjects : [subject.schema]
});
Subject Model
module.exports = mongoose.model('Course', courseSchema);
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 model
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);
What am I doing wrong in creating the entry?