title: Variable becomes undefined when access in a class method
body: <p>I defined a class variable in a constructor which holds a reference to an imported class instance.</p>
  
  <p>However, when I try to access the variable in my class methods I keep getting <code>undefined</code> for that variable.</p>
  
  <p><strong>Exported class</strong></p>
  
  <pre><code>import models from '../../database/models'
  
  const {User} = models;
  
  class UserRepository {
    constructor(){
      this.model = User;
    }
  
   async create({firstname, lastname, email, isadmin, password}){
    return await this.model.create({firstname, lastname, email, isadmin, password})
    }
  }
  
  export default new UserRepository();
  </code></pre>
  
  <p><strong>Class using the exported class</strong></p>
  
  <pre><code>import repository from './repository';
  import { sendSuccessMessage } from '../../utils'
  
  class UserController {
    constructor() {
      this.model = repository;
    }
  
    async createUser({ body }, res, next) {
      try {
        const result = await this.model.create(body);
        sendSuccessMessage(res, 200, result);
      } catch( e ){
        next(e)
      }
    }
  }
  
  export default new UserController();
  </code></pre>
  
  <p>However, using the variable <code>repository</code> directory in <code>createUser</code> as specified below:</p>
  
  <p><code>const result = await repository.create(body);</code> </p>
  
  <p>works as expected.</p>
  
  <p><strong>Current call chain</strong></p>
  
  <pre><code>// routes.js file
  import controller from './controller'
  
  export default [
    {
      path: '/signup',
      method: 'post',
      handlers: [controller.createUser] // createUser from class UserController
    }
  ]
  
  
  // utitlity.js file
  export const routesLoader = (routes, router) =&gt; {
   for (const route of routes) {
     const { path, handlers, method } = route;
     (router)[method](path, handlers);
   }
  };
  
  // app.js file
  import express from 'express';
  import routes from './routes.js';
  import { routesLoader } from './utils';
  
  const router = express.Router();
  
  routesLoader(routes, router);
  
  // server.js file
  import http from 'http';
  import app from './app';
  
  const { PORT = 3000 } = process.env;
  
  const server = http.createServer(app);
  
  server.listen(PORT, () =&gt; {
    console.log(`server listening on localhost: ${PORT}`);
  });
  </code></pre>
  
  <p><strong>Error</strong></p>
  
  <pre><code>TypeError: Cannot read property 'create' of undefined
  at _callee$ (C:\Users\MYPC\Downloads\moch-shop-api-master\components\user/controller.js:16:39)
  at tryCatch (C:\Users\MYPC\Downloads\moch-shop-api-master\node_modules\regenerator-runtime\runtime.js:45:40)
  at Generator.invoke [as _invoke] (C:\Users\MYPC\Downloads\moch-shop-api-master\node_modules\regenerator-runtime\runtime.js:271:22)
  at Generator.prototype.&lt;computed&gt; [as next] (C:\Users\MYPC\Downloads\moch-shop-api-master\node_modules\regenerator-runtime\runtime.js:97:21)
  at asyncGeneratorStep (C:\Users\MYPC\Downloads\moch-shop-api-master\node_modules\@babel\runtime\helpers\asyncToGenerator.js:3:24)
  at _next (C:\Users\MYPC\Downloads\moch-shop-api-master\node_modules\@babel\runtime\helpers\asyncToGenerator.js:25:9)
  at C:\Users\MYPC\Downloads\moch-shop-api-master\node_modules\@babel\runtime\helpers\asyncToGenerator.js:32:7
  at new Promise (&lt;anonymous&gt;)
  at C:\Users\MYPC\Downloads\moch-shop-api-master\node_modules\@babel\runtime\helpers\asyncToGenerator.js:21:12
  at createUser (C:\Users\MYPC\Downloads\moch-shop-api-master\components\user\controller.js:70:28)
  at Layer.handle [as handle_request] (C:\Users\MYPC\Downloads\moch-shop-api-master\node_modules\express\lib\router\layer.js:95:5)
  at next (C:\Users\MYPC\Downloads\moch-shop-api-master\node_modules\express\lib\router\route.js:137:13)
  at Route.dispatch (C:\Users\MYPC\Downloads\moch-shop-api-master\node_modules\express\lib\router\route.js:112:3)
  at Layer.handle [as handle_request] (C:\Users\MYPC\Downloads\moch-shop-api-master\node_modules\express\lib\router\layer.js:95:5)
  at C:\Users\MYPC\Downloads\moch-shop-api-master\node_modules\express\lib\router\index.js:281:22
  at Function.process_params (C:\Users\MYPC\Downloads\moch-shop-api-master\node_modules\express\lib\router\index.js:335:12)
  at next (C:\Users\MYPC\Downloads\moch-shop-api-master\node_modules\express\lib\router\index.js:275:10)
  at cors (C:\Users\MYPC\Downloads\moch-shop-api-master\node_modules\cors\lib\index.js:188:7)
  at C:\Users\MYPC\Downloads\moch-shop-api-master\node_modules\cors\lib\index.js:224:17
  at originCallback (C:\Users\MYPC\Downloads\moch-shop-api-master\node_modules\cors\lib\index.js:214:15)
  at C:\Users\MYPC\Downloads\moch-shop-api-master\node_modules\cors\lib\index.js:219:13
  at optionsCallback (C:\Users\MYPC\Downloads\moch-shop-api-master\node_modules\cors\lib\index.js:199:9)
  </code></pre>
  
  <p><strong>Technologies used</strong></p>
  
  <pre><code>@babel/node: "^7.8.4"
  terminal: git bash
  Os: windows
  </code></pre>