title: How to access global scope variables in callback functions? [JS]
body: <p>For my user registration I have </p>
  
  <pre><code>const express = require ('express');
  const userRouter = express.Router ();
  
  userRouter.get ('/', function getUserList (req, res) {
      let User = require ('../models').User;
      User.find ({}, function (err, list) {
          res.json (list);
      });
  });
  
  userRouter.post ('/', function createUser (req, res) {
      let User = require ('../models').User;
      if (req.body.username &amp;&amp; req.body.password)
          User.create (req.body, function (err, user) {
              res.json (user);
          });
  });
  
  ... 3 more functions with the same `let User` ...
  
  module.exports = userRouter;
  </code></pre>
  
  <p>Here, I have to <code>require</code> the module models twice. I tried setting the User variable as a global variable up at the top of the program, like </p>
  
  <pre><code>const express = ..., userRouter = ...
  var User = ...
  </code></pre>
  
  <p>However this User variable is still not accessible inside my callback functions.</p>
  
  <p>Is requiring the User module multiple times the correct way to do this or am I missing something?</p>
  
  <p><em>edit</em>: Inside the callback functions, the User variable is undefined.</p>