在节点Express中使用@httpGet批注时出错

时间:2019-12-01 21:28:45

标签: node.js typescript express

我遇到错误

  

错误TS1241:作为表达式调用时,无法解析方法装饰器的签名。”当我使用注释'@httpGet('/ product')

我导入了以下软件包:

"express": "^4.17.1",
"inversify": "^5.0.1",
"inversify-express-utils": "^6.3.2"

我的代码是:

import { controller, httpGet, request, response } from "inversify-express-utils";
import { Request, Response } from "express";

@controller("")
export class ProductController implements IProductController {

    @httpGet('/product')
    async get(@request() req: Request, @response() res: Response) {

        ....
    }
}

有人遇到过同样的问题吗?

1 个答案:

答案 0 :(得分:0)

控制器注释必须是路线的名称,get注释要求您指定查询参数,如果没有,则仅指定/。还可以尝试将interfaces.Controller用作基类。例如,尝试以下代码

import { interfaces, controller, httpGet, request, response } from "inversify-express-utils";
import { Request, Response } from "express";

@controller("/product")
export class ProductController implements interfaces.Controller {

    @httpGet('/')
    async get(@request() req: Request, @response() res: Response) { ... }

    @httpGet('/:productId')
    async get(@request() req: Request, @response() res: Response) { ... }
}