【问题标题】:TypeError: Cannot read property of undefined when I call a method from an Imported Class [TypeScript]TypeError:当我从导入的类 [TypeScript] 调用方法时无法读取未定义的属性
【发布时间】:2018-08-10 04:27:54
【问题描述】:

调试了几个小时后,我在网上也找不到任何东西..

我有一个控制器,它调用服务方法来取回数据:

路线: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

在此先感谢各位,非常感谢任何帮助!

【问题讨论】:

  • 为什么您的inspections.ts 文件中有一个尾随}inspections.ts 中的最后一个 } 可能会导致一些问题?
  • 嗨@Soufiane,有什么可以帮助你的吗?
  • 嗨@Dacre Denny,谢谢你的快速回答,我刚刚在粘贴代码时删除了一些东西,这就是原因,但在我的IDE中我没有任何额外的}:/
  • 嗨@Soufiane,我看不出有什么异常。为什么在调用 await 时不返回一个 Promise?
  • @Soufiane,我编辑了我的帖子,刚刚包含在构造函数中。

标签: javascript node.js typescript service controller


【解决方案1】:

看起来问题可能与在您的路由器中调用 inspectionsController.getInspections 的上下文有关。

也许您可以对您的路由器进行以下更改,以确保从您的 Routes 实例的上下文中调用 getInspections - 这些更改对您有用吗?

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)
        })

    }
}

【讨论】:

  • 这就像一个魅力,非常感谢你,非常感谢:D
  • 不客气!很高兴我能帮上忙——祝你的项目一切顺利:-)
【解决方案2】:

在你的控制器中试试这个,

构造函数(私人检查服务:检查服务){}

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

【讨论】:

    猜你喜欢
    • 2018-03-13
    • 2017-11-02
    • 1970-01-01
    • 2019-07-13
    • 2020-12-20
    • 1970-01-01
    • 1970-01-01
    • 2021-04-13
    • 2021-08-16
    相关资源
    最近更新 更多