When I try to access /course/new I get an error [ReferenceError: next is not defined]
.
How and where do I define next for my faulting function?
if I just add an argument next
like I've done in earlier models I just get more errors.
courseController:
module.exports = {
'new':function(req, res, err){
Student.findOne(req.param('takes'), function foundStudent (err, student){
if (err) return next(err);
if (!student) return next();
res.view({
student: student
});
});
},
create: function(req,res,next){
Course.create(req.params.all(), function courseCreated (err, course){
if(err) next(err);
res.json(course)
});
}
};
Course model:
module.exports = {
attributes: {
code:{
type: "string"
},
name:{
type:"string"
},
takes:{
model: 'student',
required: true
}
}
};
/course/new.ejs:
<form action="/course/create" method="post">
<h2>Create a new course for <%= student.name %></h2>
<input type="hidden" name="takes" value="<%= student.id%>"/>
<input type="text" placeholder="Course Code" name="code"><br/>
<input type="text" placeholder="Name" name="student"><br/>
<input type="submit" value="Add course"/>
</form>
Student schema:
//A student can have take many courses
module.exports = {
attributes: {
name:{
type: "string",
required: true
},
username:{
type:"string"
},
courses: {
collection: 'course',
via: 'takes'
}
}
};
for someone who made the same mistake as me,
I did
.get((req, res) => {
instead of
.get((req, res, next) => {
I forgot the next parameter
The problem is here:
module.exports = {
// you had err here instead of next as last argument
'new':function(req, res, next){
Student.findOne({_id:req.param('takes')}, function foundStudent (err, student){
if (err) return next(err);
if (!student) return next();
res.view({
student: student
});
});
},
create: function(req,res,next){
Course.create(req.params.all(), function courseCreated (err, course){
if(err) next(err);
res.json(course)
});
}
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With