1

My code looks something like this:

router.route('/user')
.post(function(req, res, next){

 queryDB(arg1, arg2, prepareRes)

})
.get(function(req, res, next){

queryDB(arg3, arg4, prepareRes)    

});

var prepareRes = function(err, data){

    if(err) next(err);
    else{
        req.data = data;
    }
};

when i run this code i get the following error:

ReferenceError: next is not defined

or

ReferenceError: req is not defined

This happens because req and next ,are outside prepareRes scope.

How can get around this ERROR??

I don't want to have to duplicate the same lines of code in both routes and its not possible to use

route.all

in my case.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Yaki Klein
  • 3,978
  • 3
  • 37
  • 34
  • Unlike forum sites, we don't use "Thanks", or "Any help appreciated", or signatures on [so]. See "[Should 'Hi', 'thanks,' taglines, and salutations be removed from posts?](http://meta.stackexchange.com/questions/2950/should-hi-thanks-taglines-and-salutations-be-removed-from-posts). BTW, it's "Thanks in advance", not "Thanks in advanced". – John Saunders Jan 24 '15 at 00:54

2 Answers2

1

prepareRes is declared outside of the post and get handlers so it has no access to the req(uest) or next.

The most obvious solution is to add request and next parameters to the prepareRes function signature and then, when calling prepareRes in the request handlers, to wrap the call in an anonymous function that can access them:

router.route('/user')
.post(function(req, res, next){

    queryDB(arg1, arg2, function(err, data){

        prepareRes(err,data, req, next);
    })

})
.get(function(req, res, next){

    queryDB(arg3, arg4, function(err, data){

        prepareRes(err,data, req, next);
    })

});

var prepareRes = function(err, data, req, next){

if(err) next(err);
else{
    req.data = data;
}
};

Using something like lodash you you could get rid of the anonymous functions and partially apply the additional arguments like so:

queryDB(arg1, arg2, _.partialRight(prepareRes, res, next));

But you still have to change the prepareRes signature.

Robert Moskal
  • 21,737
  • 8
  • 62
  • 86
  • Is there a way to make the function known inside post and get? Where will i need to declare it? – Yaki Klein Jan 03 '15 at 18:01
  • I'm not sure what you're asking. The above is a complete solution. – Robert Moskal Jan 03 '15 at 18:09
  • The solution works. The question is, Where are the get and post handlers declared? If i declare my function (prepareRES) in the same object that the post and get functions are declared in, I will be able to call the function with the same signature as i have now. Isn't that correct? – Yaki Klein Jan 03 '15 at 18:11
  • Oh, that depends on how you structure your application. Here's a post that shows different ways to setup your routes: http://stackoverflow.com/questions/23923365/how-to-separate-routes-on-node-js-and-express-4 – Robert Moskal Jan 03 '15 at 18:22
0

since prepareRes is not inside of any of the above functions, where req,res are provided it can not find req on its namespace, assuming req and next are not globals.

Here for instance: req and next are passed to an error handler function:

function clientErrorHandler(err, req, res, next) {
  if (req.xhr) {
    res.status(500).send({ error: 'Something blew up!' });
  } else {
    next(err);
  }
}

Then next and res is available in the scope.

David Karlsson
  • 9,396
  • 9
  • 58
  • 103