Pikachu
Pikachu

Reputation: 2003

Pass variable using next()

In node.js, i have function that before any action is checking if everything is ok at the start of request (validating JSON etc). Almost everything is working ok, but I have one problem. I don't now how to pass object reference using next();

To call checking function I'm using.

app.all('/:action', frontendProcessor.checkSession());

At the middle of this code, I'm using next()

frontendProcessor.checkSession = function(){

  return function(req, res, next) {

    var inputJson   = req.body.JSONVAR || false,
        action      = req.params.action;

    // validation   
    frontendProcessor.validateJSON(inputJson, afterValidation);

    function afterValidation(err, inputData){       
      if(err) {
        global.consoleLog(err);
        res.send(fail);
      }else{
        if(action == 'login' ){ 
          console.log(inputData);
          next(inputData); //<< here, how to pass inputData thru next
        }else{
          mUsers.checkSessionId(email, sessionId, process);
        };      
      };        
    };

    function process(response) {        
      if(!response){
        global.consoleLog("Security Error: Bad session Id.");
        var response = JSON.stringify(badSession);
        res.send(response);
      }else{
        global.consoleLog('Security: session ok! next');
        next(inputData);
      };
    };

  };

};

Upvotes: 1

Views: 798

Answers (1)

Tim Brown
Tim Brown

Reputation: 3241

next() shouldn't ever pass data because it's just designed to call the next request handler, not a specific request handler. Nirk's comment is correct, you should attach your data to the req object and read it from there when needed.

Upvotes: 1

Related Questions