Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Next is not defined, but I don't understand how to define it in my function

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 nextlike 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'
    }
  }

};
like image 370
Gurkmeja101 Avatar asked Apr 20 '16 12:04

Gurkmeja101


Video Answer


2 Answers

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

like image 159
Mo D Genesis Avatar answered Oct 24 '22 10:10

Mo D Genesis


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)
 });
}

};
like image 38
AJS Avatar answered Oct 24 '22 10:10

AJS