【问题标题】:status code must be a number Error on POST request tsoa typescrpt状态码必须是数字 POST 请求 tsoa typescript 上的错误
【发布时间】:2020-06-10 03:48:41
【问题描述】:

我正在试验 tsoa 的其余 API。但是对于post 请求,我总是收到以下错误。

AssertionError [ERR_ASSERTION]: status code must be a number
      at Object.set status [as status] (PROJECT_PATH/node_modules/koa/lib/response.js:87:5)
      at Object.status (PROJECT_PATH/node_modules/delegates/index.js:92:31)
      at Object.<anonymous> (PROJECT_PATH/src/rest/routes.ts:93:31)
      at Generator.next (<anonymous>)
      at PROJECT_PATH/src/rest/routes.ts:8:71
      at new Promise (<anonymous>)
      at __awaiter (PROJECT_PATH/src/rest/routes.ts:4:12)
      at RegisterRoutes.router.post (PROJECT_PATH/src/rest/routes.ts:84:43)
      at dispatch (PROJECT_PATH/node_modules/koa-compose/index.js:42:32)
      at PROJECT_PATH/node_modules/koa-router/lib/router.js:353:16

示例代码如下

  @Post()
  public async createUser(@BodyProp() requestBody: UserInput): Promise<void> {
    console.log(requestBody)
    // code
    this.setStatus(201);
    return Promise.resolve();
  }

在我的tsoa.json 配置中,我添加了"middleware": "koa",,因为我使用了kao

【问题讨论】:

  • 这是整个错误信息吗?看起来第一部分不见了。

标签: typescript swagger koa tsoa


【解决方案1】:

我最近遇到了同样的错误。我设法通过添加koa-bodyparser 中间件来修复它。它确保将 http 正文添加到 koa ctx.request 对象中。

我在tsoa 文档中找不到任何提及。

示例代码:

    @Post('')
    public async handleRequest (
        @Request() req: Request,
        @BodyProp('param_1') param1: number
    ): Promise<void> {
        // These will all print out the http body
        console.log(param1)
        console.log(req.body)
        console.log(req.ctx.request.body)
    }

【讨论】:

    猜你喜欢
    • 2018-09-30
    • 2018-10-23
    • 2022-01-04
    • 2018-07-12
    • 1970-01-01
    • 1970-01-01
    • 2015-01-09
    • 2022-11-21
    • 2019-09-12
    相关资源
    最近更新 更多