Nestjs:无法在同一个控制器中实现多个 GET 方法
Nestjs: Cant implement mutiple GET methods in same controller
我这里有一些奇怪的问题。我有 2 种获取方法,一种是通过 companyId 获取员工列表,通过 staffId 获取员工详细信息。第二种方法 getStaffById
不起作用。如果我注释掉第一个 get 方法 getStaffByCompanyId
,第二个按预期工作。看起来第一个 get 方法阻塞了第二个方法。我错过了什么吗?
@Controller('staff')
@ApiTags('Staff')
export class StaffController {
constructor(private staffService: StaffService) {}
@Get(':companyId')
@ApiResponse({
status: HttpStatus.OK,
description: 'Return staffs by id',
})
@ApiResponse({
status: HttpStatus.UNAUTHORIZED,
description: 'Unauthorized',
type: Error,
})
@ApiResponse({
status: HttpStatus.UNPROCESSABLE_ENTITY,
description: 'Get particular staff details',
type: Error,
})
@ApiResponse({
status: HttpStatus.INTERNAL_SERVER_ERROR,
description: 'Internal server error',
type: Error,
})
@ApiResponse({
status: HttpStatus.BAD_GATEWAY,
description: 'Internal communication error',
type: Error,
})
@ApiOperation({
operationId: 'getStaffByCompanyId',
summary: 'Get staff list that attach to the company',
})
async getStaffByCompanyId(@Param('companyId') companyId: string): Promise<IStaff[]> {
return await this.staffService.getStaffByCompanyId(companyId);
}
@Get(':staffId')
@ApiResponse({
status: HttpStatus.OK,
description: 'Return staff by id',
})
@ApiResponse({
status: HttpStatus.UNAUTHORIZED,
description: 'Unauthorized',
type: Error,
})
@ApiResponse({
status: HttpStatus.UNPROCESSABLE_ENTITY,
description: 'Get particular staff details',
type: Error,
})
@ApiResponse({
status: HttpStatus.INTERNAL_SERVER_ERROR,
description: 'Internal server error',
type: Error,
})
@ApiResponse({
status: HttpStatus.BAD_GATEWAY,
description: 'Internal communication error',
type: Error,
})
@ApiOperation({
operationId: 'getStaffById',
summary: 'Get staff profile details',
})
async getStaffById(@Param('staffId') staffId: string): Promise<IStaff> {
return await this.staffService.getStaffById(staffId);
}
}
Express(Nest 最有可能在后台为您使用的)无法区分
app.get('/:companyId', (req, res, next) => {})
和
app.get('/:staffId', (req, res, next) => {})
因为它只能看到将有一个请求,其中某种通用参数作为字符串传入,所以虽然您可能知道 companyId
以 C
开头staffId
以 S
开头,无法告诉 Express,因此,也无法告诉 Nest 告诉 Express。你能做的最好的事情就是在你的路由中添加一个前缀,比如 /comapny/:companyId
和 /staff/:staffId
,以确保它们保持分开。
我这里有一些奇怪的问题。我有 2 种获取方法,一种是通过 companyId 获取员工列表,通过 staffId 获取员工详细信息。第二种方法 getStaffById
不起作用。如果我注释掉第一个 get 方法 getStaffByCompanyId
,第二个按预期工作。看起来第一个 get 方法阻塞了第二个方法。我错过了什么吗?
@Controller('staff')
@ApiTags('Staff')
export class StaffController {
constructor(private staffService: StaffService) {}
@Get(':companyId')
@ApiResponse({
status: HttpStatus.OK,
description: 'Return staffs by id',
})
@ApiResponse({
status: HttpStatus.UNAUTHORIZED,
description: 'Unauthorized',
type: Error,
})
@ApiResponse({
status: HttpStatus.UNPROCESSABLE_ENTITY,
description: 'Get particular staff details',
type: Error,
})
@ApiResponse({
status: HttpStatus.INTERNAL_SERVER_ERROR,
description: 'Internal server error',
type: Error,
})
@ApiResponse({
status: HttpStatus.BAD_GATEWAY,
description: 'Internal communication error',
type: Error,
})
@ApiOperation({
operationId: 'getStaffByCompanyId',
summary: 'Get staff list that attach to the company',
})
async getStaffByCompanyId(@Param('companyId') companyId: string): Promise<IStaff[]> {
return await this.staffService.getStaffByCompanyId(companyId);
}
@Get(':staffId')
@ApiResponse({
status: HttpStatus.OK,
description: 'Return staff by id',
})
@ApiResponse({
status: HttpStatus.UNAUTHORIZED,
description: 'Unauthorized',
type: Error,
})
@ApiResponse({
status: HttpStatus.UNPROCESSABLE_ENTITY,
description: 'Get particular staff details',
type: Error,
})
@ApiResponse({
status: HttpStatus.INTERNAL_SERVER_ERROR,
description: 'Internal server error',
type: Error,
})
@ApiResponse({
status: HttpStatus.BAD_GATEWAY,
description: 'Internal communication error',
type: Error,
})
@ApiOperation({
operationId: 'getStaffById',
summary: 'Get staff profile details',
})
async getStaffById(@Param('staffId') staffId: string): Promise<IStaff> {
return await this.staffService.getStaffById(staffId);
}
}
Express(Nest 最有可能在后台为您使用的)无法区分
app.get('/:companyId', (req, res, next) => {})
和
app.get('/:staffId', (req, res, next) => {})
因为它只能看到将有一个请求,其中某种通用参数作为字符串传入,所以虽然您可能知道 companyId
以 C
开头staffId
以 S
开头,无法告诉 Express,因此,也无法告诉 Nest 告诉 Express。你能做的最好的事情就是在你的路由中添加一个前缀,比如 /comapny/:companyId
和 /staff/:staffId
,以确保它们保持分开。