جاوااسکریپت - خطای TypeError: نمیتوان خاصیت را از مقدار undefined خواند هنگام فراخوانی یک متد از یک کلاس وارد شده [TypeScript]


بعد از چند ساعت رفع اشکال این مسئله، من هم نتوانستم چیزی آنلاین پیدا کنم..

من یک کنترلر دارم که یک متد سرویس را برای بازگرداندن دادهها فراخوانی میکند:

مسیر: routes/inspections.ts

import {Request, Response} from "express";
import { InspectionsController } from "../controllers";

export class Routes {

    public inspectionsController: InspectionsController = new InspectionsController()
    public  routes(app): void {


        // Inspection detail
        app.route('/data/inspections/:inspectionId')
        // get specific inspection
            .get(this.inspectionsController.getInspectionByID)

        // Inspection detail
        app.route('/data/inspections')
        // get specific inspection
            .get(this.inspectionsController.getInspections)

    }
}

کنترلکننده: controllers/inspections.ts

    import { Request, Response } from 'express';
    import { InspectionsService } from '../services/inspections';

    export class InspectionsController {
        public inspectionsService: InspectionsService = new InspectionsService();
        public async getInspections (req: Request, res: Response) {
            try {
                const response = await this.inspectionsService.getInspections();
                res.json(response);
            }catch (e) {
                console.log(e.toString())
            }
        }

        public getInspectionByID (req: Request, res: Response) {
            res.json('You reached getInspectionByID in the controller Wohoooo!');
        }
}

سرویس: services/inspections.ts

    export class InspectionsService {
     public  async getInspections(){
         return 'You reached getInspections in the service Wohoooo!';
    }
}

مشکل من این است که برنامهام تنها به سطح کنترلر میرسد و به سرویس نمیرسد. من پیام خطای زیر را دریافت میکنم:

TypeError: Cannot read property 'inspectionsService' of undefined

پیشاپیش از همه شما خیلی ممنونم، هر کمکی واقعاً قدردانی میشود!

پاسخ شماره ۱

به نظر میرسد که مشکل ممکن است مربوط به زمینهای باشد که inspectionsController.getInspections از آن در روتر شما فراخوانی میشود.

شاید بتوانید تغییرات زیر را در روتر خود اعمال کنید تا مطمئن شوید که getInspections از بافت نمونه Routes شما فراخوانی میشود - آیا این تغییرات برای شما کار میکند؟

export class Routes {

    public inspectionsController: InspectionsController = new InspectionsController()
    public  routes(app): void {


        // Inspection detail
        app.route('/data/inspections/:inspectionId')
        // get specific inspection
            .get(this.inspectionsController.getInspectionByID)

        // Inspection detail
        app.route('/data/inspections')
        // get specific inspection
        .get((req: Request, res: Response) => {

          // UPDATE: Move the invocation of getInspections() into an arrow function
          // so that the "this" context is that of the Routes instance
          this.inspectionsController.getInspections(req, res)
        })

    }
}

پاسخ شماره ۲

این را در کنترلر خود امتحان کنید،

constructor(private inspectionsService: InspectionsService) {}

public getInspections(){
  return new Promise(resolve => {
      setTimeout(() => {
        resolve('You reached getInspections in the service Wohoooo!');
      }, 2000);
  });
}