如何处理NestJS@get()装饰器? [英] How to deal with NestJS @Get() decorator?

查看:65
本文介绍了如何处理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()装饰器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆