我有一个使用 @Controller('tasks')装饰器的控制器。在此控制器内部,我有一条路由 @Get('/ week'),通常该请求应发送到 localhost:4000 / tasks / week ,但它返回的是错误的请求:
{
"statusCode": 400,
"error": "Bad Request",
"message": "Validation failed (numeric string is expected)"
}
下面是我的代码:
@Controller('tasks')
@UseGuards(AuthGuard())
export class TasksController {
constructor(private tasksService: TasksService) { }
@Get('/:id')
getTaskById(@Param('id', ParseIntPipe) id: number): Promise<Task> {
return this.tasksService.getTaskById(id);
}
@Get('/week')
getTasksByWeek(@GetUser() user: User): Promise<Task[]> {
return this.tasksService.getTasksByWeek(user);
}
从Get()装饰器中删除 /周可以,但不能添加。
预期结果:返回数据
实际结果:
{
"statusCode": 400,
"error": "Bad Request",
"message": "Validation failed (numeric string is expected)"
}
答案 0 :(得分:0)
我找到了解决此问题的方法。诸如Get('week')之类的路由或接受参数的路由应在带有基本控制器路由的路由之前排成一行。
假设我们有一条@GET()路由,如下所示,它获取了所有任务:
@Get()
getTasks(@GetUser() user: User): Promise<Task[]> {
return this.tasksService.getTasksByWeek(user);
}
代码中应如何显示
@Controller('tasks')
@UseGuards(AuthGuard())
export class TasksController {
constructor(private tasksService: TasksService) { }
@Get('/:id')
getTaskById(@Param('id', ParseIntPipe) id: number): Promise<Task> {
return this.tasksService.getTaskById(id);
}
@Get('/week')
getTasksByWeek(@GetUser() user: User): Promise<Task[]> {
return this.tasksService.getTasksByWeek(user);
}
@Get()
getTasks(@GetUser() user: User): Promise<Task[]> {
return this.tasksService.getTasksByWeek(user);
}
答案 1 :(得分:0)