How to deal with NestJS @Get() decorator?(如何处理NestJS@get()装饰器?)
本文介绍了如何处理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()装饰器?
猜你喜欢
- Quasar 2+Apollo:错误:找不到ID为默认的Apollo客户端。如果您在组件设置之外,请使用ProvideApolloClient() 2022-01-01
- 如何使用 JSON 格式的 jQuery AJAX 从 .cfm 页面输出查 2022-01-01
- 使用RSelum从网站(报纸档案)中抓取多个网页 2022-09-06
- Css:将嵌套元素定位在父元素边界之外一点 2022-09-07
- 400或500级别的HTTP响应 2022-01-01
- addEventListener 在 IE 11 中不起作用 2022-01-01
- 失败的 Canvas 360 jquery 插件 2022-01-01
- Flexslider 箭头未正确显示 2022-01-01
- CSS媒体查询(最大高度)不起作用,但为什么? 2022-01-01
- Fetch API 如何获取响应体? 2022-01-01
