Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

help with next(err)

I know what next do (passing to the next middleware function), but I dont know what does the next(err) do. I know it's for error handling, but the theory of this is my problem... because in the part when theacher makes the error handler in the vote route, uses the 2 types of next, one with err inside and the other one without it. So what happened, err passes as an argument? where do the code is redirecting me?

router.post("/:qID/answers/:id/:dir-vote",function(req,res){
        if(req.params.dir.search("/^(up|down)$/")==-1){
            var err= new Error("Not Found");
            err.status=404;
            next(err);
        }else{
            next();
        }
},function(req,res,next){


    res.json({response:"me enviaste una peticion POST en /answers/id/votes y votaste vote-"+req.params.dir,
    questionId:req.params.qID,
    anserId:req.params.id,
    vote:req.params.dir
    });
    console.log("respuesta mostrada ");
});```