【发布时间】: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