title: TypeError: Cannot read property 'unitService' of undefined
body: <p>I have this class in TypeScript and when I call <code>this.unitService</code> I get an error.</p>
  
  <p>controller.ts</p>
  
  <pre><code>import { Request, Response } from "express";
  import Unit from "../models/unit.model";
  import Relation from "../models/relation.model";
  import { TypeRelation } from "../models/typeralation.enum";
  import { UnitService } from "../services/unit.service";
  
  export class HomeController {
    unitService: UnitService = new UnitService();
    constructor() { }
  
    forceCreateUnit(req: Request, res: Response): void {
      const unit = "{ type: " + TypeRelation.USE + ", topUnit: new ObjectId(\"5adf231d85dded040f3f6d03\"), lowerUnit: new ObjectId(\"5adf231d85dded040f3f6d03\")}";
      this.unitService.prueba();
      res.send("OK");
    }
  }
  </code></pre>
  
  <p>unit.service.ts</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>import Relation from "../models/relation.model";
  import { TypeRelation } from "../models/typeralation.enum";
  import { ObjectId } from "bson";
  
  export class UnitService {
      constructor() {}
      async forceGenerate(unit: String) {
          const relation = new Relation(
              {
                type: TypeRelation.USE,
                topUnit: new ObjectId("5adf231d85dded040f3f6d03"),
                lowerUnit: new ObjectId("5adf231d85dded040f3f6d03")
              }
            );
            await relation.save();
      }
  }</code></pre>
  </div>
  </div>
  </p>
  
  <p>However when I put <code>new unitService()</code> in <code>forceCreateUnit()</code>, it works good. Why is that?</p>
  
  <p>I call method like this:</p>
  
  <pre><code>const homeController: HomeController = new HomeController();
  
  homeRoutes.get("/prueba", homeController.forceCreateUnit);
  </code></pre>