title: Node.js Express How to pass data between middleware?
body: <p>I am trying to send data in MEAN Stack in Node/Express from one middleware to another, I have a route set up, where I have 2 named functions, one queries some data and passes it to a second function that should do some other processing on the data... here is my routing function that has the 2 middleware functions on the "get" method:</p>
  
  <pre><code>function DemonstratorRoutes(router) {
      var DemonstratorController = require('../controllers/DemonstratorController')
      router.route('/Demonstrator')
        .get(DemonstratorController.list_all_Demonstrator, DemonstratorController.doSomeStuff)
  module.exports = DemonstratorRoutes;
  </code></pre>
  
  <p>the implementation of the functions are as follows, its just simple stuff for demonstration / try out purpose..:</p>
  
  <pre><code>exports.list_all_Demonstrator = function(req, res,next) {
      //console.log(req.body)
      Demonstrator.find({},  function(err, demo) {
          if (err){
              console.log(err);}
          else {
              res.locals.myvar = demo;    
          }
      });
      next();
  };
  
  exports.doSomeStuff = function(req,res,next) {
      var data;
      data = res.locals.myvar
      console.log("Dies ist 2te Funktion:", data);
      res.send(data);
  }
  </code></pre>
  
  <p>I have seen that example somewhere... however they did implement it otherwise, they just defined anonymous functions one after the other, separated by commas, but I would like to use named functions and use them in the route file as parameters for Express routing function middleware..
  <strong>However, I have the problem that the data in the second function is "undefined", I dont understand why this does not work, has it to do with function hoisting? Maybe I have to define my functions otherwise? Or is my callback wrong?</strong></p>