Thursday, 16 March 2017

node.js callback design - how to use res in callback

I have a route that do some processing than it should return a callback. here is the code:

router.get('/login', function(req,res,next) {
  // send call to login function.
  var ID= req.query.ID;
  var Password = req.query.Password 

  ctlLogin.login(ID,Password, LoginCallback);



});

function LoginCallback(err,myLoginResult)
{
  res.json(myLoginResult);
}

The Login function check if the user/pass exists in the database and returns:

loginCallback(result);

My question - the LoginCallBack fucntion does not know the object res. I know I can transfer it to the login module just to have it passed back to me as a parameter to callback function, but it seems like a bad design, it's bad enough I need to send the callback function to the login method.

Is there a better way to do it, and still have readable code ?



via Dani

No comments:

Post a Comment