title: Getting error next is not a function in node js / express js
body: <p>I am getting this error: </p>
  
  <blockquote>
    <p>next is not a function</p>
  </blockquote>
  
  <p>when I write <code>next();</code> in my controller but can't figure out why is the error coming even though I defined it in the function params.</p>
  
  <p>Router Js:</p>
  
  <pre><code>router.post('/era',controlleri.book,booking_controller.book,function(req,res){
  res.writeHead(200, {'Content-Type': 'text/plain'});
                            res.end('okay');
      console.log("done in server.js");
  });
  </code></pre>
  
  <p>This is my router file in which I want to carry out function synchronously so thats why I am using <code>next();</code>.</p>
  
  <p>Controller.Book function code:</p>
  
  <pre><code>   module.exports.book=function(req,res,next){
      var dates=req.body.dates;
      var times=req.body.times;
      var result="yes";
  
      var query=`dates.${dates}.${times}.ava`;
  
      db.mohd.find({[query]:result},{userid:1,_id:0},function (err, docs) {
          if(err){
  
              res.send(404);
  
          }else if(docs==null){
  
              res.send(404);   
  
           }else{
                   if(req.body){
                        var thid=req.body.therapist_id;
                        var dates=req.body.dates;
                        var times=req.body.times;
                        var query=`dates.${dates}.${times}.ava`;
  
                       db.mohd.update( { userid: thid },
                         { $set: { [query]: "no" } }, function () {
                              console.log("updated");
                      });
                          **next();** //Here the error occurs
                  }else{
                          res.send("Get the F out of here");
                  }
          }
  });}
  </code></pre>
  
  <p>Although the next function i.e <code>booking_controller.book</code> is executed but the program stops by giving the error: </p>
  
  <blockquote>
    <p>next is not a function</p>
  </blockquote>