title: Why does assigning a MySQL pool to a member variable revert to undefined when a member function is called?
body: <p>I would like to create one connection pool that every query will use. To do so, I've created a very simple class that assigns the connection pool to a member variable. The member variable is correctly set in the constructor, but when a member function is called that references the pool member variable, pool is null.</p>
  <p>NOTE: I'm intentionally withholding credentials. Rest assured that it's correct on my end. :)</p>
  <pre><code>// Promise wrapper for MySQL driver
  class Database {
      constructor( connectionLimit = 10 ){
          this.pool = mysql.createPool({
              connectionLimit : connectionLimit,
              host            : 'XXX',
              user            : 'XXX',
              password        : 'XXX',
              database        : 'XXX'
          });
  
          console.log( this.pool ) // No problem!
      }
  
      query( sql, params ){
          console.log( this.pool ) //UNDEFINED!!!!
  
          // Irrelevant code removed
      }
  }
  </code></pre>
  <p>Now, if I define pool on the module level instead of in the class constructor, everything works as expected.</p>
  <pre><code>const pool = mysql.createPool({
      connectionLimit : 10,
      host            : 'XXX',
      user            : 'XXX',
      password        : &quot;XXX&quot;,
      database        : 'XXX'
  });
  
  // Promise wrapper for MySQL driver
  class Database {
      constructor( connectionLimit = 10 ){
          console.log( this.pool ) // No problem!
      }
  
      query( sql, params ){
          console.log( this.pool ) // No problem!
          
          // Irrelevant code removed
      }
  }
  </code></pre>
  <p>In the first example, I expect <code>this.pool</code> to refernce the pool created in the constructor regardless of which member function is called. The second example does exactly what I would expect it to do.</p>
  <p>This class is being used in an express API application. I'm creating an instance of Database, and then assigning a reference to the query method to the req object.</p>
  <pre><code>const db = new Database();
  app.use( ( req, res, next ) =&gt; {
      req.query = db.query // Pretty sure I'm borking up the context here
  });
  </code></pre>