title: es6 class variable cannot be read (cannot read property '...' of undefined)
body: <p>So I just dove into es6 for the first time and wanted to use the new Classes, but I'm running into a strange problem. All classes work fine and write/read their class variables just fine. But one class is not doing so well.</p>
  
  <p>I'm currently getting this error: </p>
  
  <p><code>TypeError: Cannot read property 'product' of undefined
  at getProducts (***/controllers/ProductController.js:41:13)</code></p>
  
  <p>I'm writing a REST API for a school assignment using the MEAN Stack.</p>
  
  <p>In short what's currently happening</p>
  
  <p><em>index.js</em></p>
  
  <pre><code>// dependencies
  const Api  = require('./routes/api')
  
  class Rest
  {
      constructor()
      {
          this.api = new Api()
          this.init()
      }
  
      init()
      {
          ... // express server configuration
  
          // routes
          this.routes()
      }
  
      // set routes
      routes()
      {
          app.use('/api', this.api.getRouter())
      }
  }
  
  new Rest()
  </code></pre>
  
  <p><em>/routes/api.js</em></p>
  
  <pre><code>// dependencies
  const express = require('express')
  const Products = require('./api/products')
  
  class Api
  {
      constructor()
      {
          this.router = express.Router()
          this.products = new Products()
          this.routes()
      }
  
      getRouter() { return this.router }
  
      routes()
      {
          // product routes
          this.router.use('/products', this.products.getRouter())
      }
  }
  
  
  // return routes
  module.exports = Api
  </code></pre>
  
  <p><em>routes/api/products.js</em></p>
  
  <pre><code>// dependencies
  const express = require('express')
  const productController = require('../../controllers/ProductController')
  
  class Product
  {
      constructor()
      {
          this.router = express.Router()
          this.controller = new productController()
          this.routes()
      }
  
      getRouter() { return this.router }
  
      // set header options
      setCollectionOptions(req, res, next)
      {
          res.header('Access-Control-Allow-Methods', 'POST, GET, OPTIONS')
          next()
      }
  
      // set routes
      routes()
      {
          this.router.route('/')
              .options([this.setCollectionOptions, this.controller.getOptions])
              .get([this.setCollectionOptions, this.controller.getProducts])
      }
  }
  
  // return routes
  module.exports = Product
  </code></pre>
  
  <p><em>models/Product.js</em></p>
  
  <pre><code>// dependencies
  const mongoose = require('mongoose')
  const mongoosePaginate = require('mongoose-paginate')
  
  // create schema for model
  const productSchema = new mongoose.Schema({
      name: String,
      sku: String,
      price: String,
      created_at: { type: String, default: Date.now },
      updated_at: { type: String, default: Date.now }
  })
  productSchema.plugin(mongoosePaginate)
  
  // export model
  module.exports = mongoose.model('Products', productSchema)
  </code></pre>
  
  <p><em>controllers/ProductController.js</em></p>
  
  <pre><code>// dependencies
  const express = require('express')
  
  class ProductController
  {
      constructor()
      {
          this.product = require('../models/product')
      }
  
  
      getProducts(req, res, next)
      {
          this.product.find() // error occurs here!
          ... // rest of the code
      }
  }
  </code></pre>
  
  <p>The error occurs at <code>this.product.find()</code></p>
  
  <p>when I <code>console.log(this.product)</code>, immediately after I set it, it returns just fine. But when I request the page with a GET at <code>http://localhost:port/api/products</code> I receive this error.</p>
  
  <p>Also, when I try to use a method in the ProductController it results in the same error. For example:</p>
  
  <pre><code>class ProductController
  {
      constructor()
      {
          this.product = require('../models/product')
      }
  
      init()
      {
          console.log('hi!')
      }
  
      getProducts(req, res, next)
      {
          this.init()
  
          ... // rest of code
      }
  }
  </code></pre>
  
  <p>Thanks in advance.</p>