How to deal with NestJS @Get() decorator?(如何处理NestJS@get()装饰器?)
问题描述
此挡路代码工作正常。我可以通过URL访问这两个函数 http://localhost:3000/vehicle/availableVehicles 相应地&;http://localhost:3000/vehicle/1
@Controller('vehicle')
export class VehicleController {
constructor(
private readonly vehicleService: VehicleService,
private readonly crudService: CurdService
) { }
tableName: string = 'vehicle';
@Get('availableVehicles')
async availableVehicles() {
return await this.vehicleService.availableVehicles();
}
@Get(':id')
async getbyId(@Req() request: Request) {
return await this.crudService.getById(this.tableName, request.params.id);
}
}
但是,当我只是在两个函数(如下面的代码挡路)之间切换时,availableVehicles()函数不起作用,http://localhost:3000/vehicle/availableVehicles命中getbyId()函数。该怎么办呢?或者我做错了什么?提前谢谢。
@Controller('vehicle')
export class VehicleController {
constructor(
private readonly vehicleService: VehicleService,
private readonly crudService: CurdService
) { }
tableName: string = 'vehicle';
@Get(':id')
async getbyId(@Req() request: Request) {
return await this.crudService.getById(this.tableName, request.params.id);
}
@Get('availableVehicles')
async availableVehicles() {
return await this.vehicleService.availableVehicles();
}
}
推荐答案
您只需完全按照第一个示例所做的操作,将更具体的路由放在接受路由参数的路由之上。
在应用程序启动时构建服务器的路由表时,将按此顺序发现并注册它们。
这是https://stackoverflow.com/a/68727403/1364771的副本
这篇关于如何处理NestJS@get()装饰器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何处理NestJS@get()装饰器?
基础教程推荐
- 最佳动态 JavaScript/JQuery 网格 2022-01-01
- 当木偶师打开Chrome时,不能使用Chrome扩展 2022-01-01
- 带角度的选项卡:仅使用 $http 在单击时加载选项卡 2022-01-01
- 从快速中间件中排除路由 2022-01-01
- 在 Javascript 中使用 Fetch API 上传文件并显示进度 2022-01-01
- HTML5 画布调整为父级 2022-01-01
- CORS:当凭据标志为真时,无法在 Access-Control-Allow-Origin 中使用通配符 2022-01-01
- 即使每次插入第一个输入的值不同,第二个输入仍显示相同的输入值 2022-01-01
- 使用 jQuery 在悬停时交换 DIV 类 2022-01-01
- 逻辑运算符 ||在 javascript 中,0 代表 Boolean false? 2022-01-01
