状态代码必须是数字POST请求出错tsoa typescrpt



我正在为其余API试验tsoa。但对于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

我最近遇到了同样的错误。我设法通过添加koa-bodysparser中间件来修复它。它确保http主体被添加到koactx.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)
}

相关内容

  • 没有找到相关文章