title: Node.js ES6 Class unable to call class method from within class method when using Express.js
body: <p><strong>Nb.</strong> This is driving me a little nuts and I've been around the houses a few times. However I'm fairly new to ES6 and JS as a whole and fully understand that a JS Class is not like that of Classes found in other languages and might be approaching this completely wrong. </p>
  
  <p>I'm running the following code which is using Express.js (v4.16.3) and body-parser (v1.18.2) on Node v8.9.0. </p>
  
  <pre><code>app.post('/api/v1/user/update', urlencodedParser, user.update);
  </code></pre>
  
  <p>The code calls 'urlencodedParser' which is acting as middleware to provide 'req' with 'req.body' so that I can pull out the form fields. 'user' is a class module that has been exported and contains all the functions for validation, updating etc. and looks something like this:</p>
  
  <pre><code>class Users {    
      update(req,res) {
          console.log('updating...');
          this.verifyUserIdentity();
      }
  
      verifyUserIdentity(req,res) {
          console.log('verify');
      }
  }
  
  module.exports = new Users;
  </code></pre>
  
  <p>Now if I were to run this code in node without Express like so:</p>
  
  <pre><code>var users = require('./modules/users');
  
  users.update();
  </code></pre>
  
  <p>It all appears to execute and I get the following output on the CLI:</p>
  
  <pre><code>updating...
  verify
  </code></pre>
  
  <p>If I wrap it all up in the <em>app.post()</em> (above) and use Postman to send a POST it executes the first method and stops dead after the <em>console.log()</em> with no error. It seems not to call <em>verifyUserIdentity()</em> and I get the following on the CLI:</p>
  
  <pre><code>updating...
  </code></pre>
  
  <p>If I modify the code as you see below and pass an array of methods to Express' middleware handler, it seems to work, but now I have to call <em>verifyUserIdentity()</em> separately, and doesn't solve the problem of how do I call another method from the same class, for instance a <em>log()</em> method.</p>
  
  <pre><code>class Users {    
      update(req,res) {
          console.log('updating...');
      }
  
      verifyUserIdentity(req,res,next) {
          console.log('verify');
          next();
      }
  }
  
  module.exports = Users;
  
  
  
  app.post('/api/v1/user/update', [urlencodedParser, users.verifyUserIdentity], users.update);
  </code></pre>
  
  <p>Some my question:
   - Why isn't the original pattern working with Express?
   - has 'this' taken a hike because of the callback handlers?
   - Has this got something to do with Node v8.9.0?
   - Am I doing this all wrong?</p>