Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript TypeError:从导入的类[TypeScript]调用方法时,无法读取undefined的属性_Javascript_Node.js_Typescript_Service_Controller - Fatal编程技术网

Javascript TypeError:从导入的类[TypeScript]调用方法时,无法读取undefined的属性

Javascript TypeError:从导入的类[TypeScript]调用方法时,无法读取undefined的属性,javascript,node.js,typescript,service,controller,Javascript,Node.js,Typescript,Service,Controller,调试了几个小时后,我在网上也找不到任何东西 我有一个控制器,它调用服务方法以获取数据: 路线:路线/检查。ts import {Request, Response} from "express"; import { InspectionsController } from "../controllers"; export class Routes { public inspectionsController: InspectionsController = new Inspectio

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

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

路线:路线/检查。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)

    }
}
    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!');
        }
}
    export class InspectionsService {
     public  async getInspections(){
         return 'You reached getInspections in the service Wohoooo!';
    }
}
控制器:控制器/检查。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)

    }
}
    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!');
        }
}
    export class InspectionsService {
     public  async getInspections(){
         return 'You reached getInspections in the service Wohoooo!';
    }
}
服务:服务/检查。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)

    }
}
    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!');
        }
}
    export class InspectionsService {
     public  async getInspections(){
         return 'You reached getInspections in the service Wohoooo!';
    }
}
我的问题是,我的程序只达到控制器级别,而没有达到服务级别。 我收到以下错误消息:

TypeError: Cannot read property 'inspectionsService' of undefined

非常感谢各位,任何帮助都是非常感谢的

在控制器中尝试此操作

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

public-getInspections(){
返回新承诺(解决=>{
设置超时(()=>{
resolve('您在服务wohooo!中找到了getInspections!');
}, 2000);
});

}
看起来问题可能与路由器中调用的
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)
        })

    }
}

为什么在您的
inspections.ts
文件中有一个尾随
}
?最后一个
}
检查中。ts
可能会引起一些问题?嗨@Soufiane我能帮你什么忙吗?嗨@Dacre Denny,谢谢你的快速回答,巴德“我只是在粘贴代码时删除了一些东西,这就是为什么,但在我的IDE中我没有任何额外的东西。):/嗨@Soufiane,我看不到任何异常。为什么你不在打电话给Wait的时候回复一个承诺?@Soufiane,我编辑了我的帖子,刚刚被收录在构造器中。这就像一个符咒,非常感谢你,巴德”,非常感谢:不客气!很高兴我能为您提供帮助-祝您的项目一切顺利:-)