问题描述
我这里有一些奇怪的问题。我有两种获取方法,一种是通过 companyId 获取员工列表并通过员工 ID 获取员工详细信息。第二种方法 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',})
@ApiResponse({
status: HttpStatus.INTERNAL_SERVER_ERROR,description: 'Internal server error',})
@ApiResponse({
status: HttpStatus.BAD_GATEWAY,description: 'Internal communication 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',})
@ApiOperation({
operationId: 'getStaffById',summary: 'Get staff profile details',})
async getStaffById(@Param('staffId') staffId: string): Promise<IStaff> {
return await this.staffService.getStaffById(staffId);
}
}
解决方法
Express(Nest 最有可能在幕后为您使用的)无法区分
class Category(models.Model):
title = models.CharField(max_length=200,unique=True,blank=False,null=False)
class SubCategory(models.Model):
title = models.CharField(max_length=200,unique=False,blank=False)
category = models.ForeignKey(Category,related_name='subcategories',on_delete=models.CASCADE)
和
app.get('/:companyId',(req,res,next) => {})
因为它只能看到会有一个请求,带有某种通用参数作为字符串传入,所以虽然您可能知道一个 app.get('/:staffId',next) => {})
以一个 companyId
开头和一个C
以 staffId
开头,无法告诉 Express,因此,也无法告诉 Nest 告诉 Express。您能做的最好的事情是在您的路由中添加一个前缀,如 S
和 /comapny/:companyId
,以确保它们保持分离。