Ale
Ale

Reputation: 1

Nest js controller @Get endpoint doesn't execute associated function

I have this controller:

@UseGuards(AuthGuard("api-jwt"))
@ApiTags("Delivery")
@Controller("delivery")
export class DeliveryController {
    constructor(private readonly orderService: OrderService) {}

}

i would like to add this endpoint:

@Get("/stats")
    @ApiOperation({ summary: "Get Delivery Stats", description: "Get deliveries stats by params" })
    async getDeliveryStats(@Req() req, @Query("start") start: string, @Query("end") end: string): Promise<any> {
        let idCompany = req.user.id;
        return await this.orderService.getDeliveryStats(start, end, idCompany);
    }

But when i contact it the func getDeliveryStats of this controller doesn't work. The call passed the guard, but if i conole.log('hello world') into the func it doesn't log and i don't receve any reponce or errors. If i replace @Get with @Put it works correctly.

As u can see i refer to @Req req and get the user from there, because in my guard validate func i return a custom entity and i should get the id, this works for other endpoints

I tried to check the guard validation func and that execute correctly. I tried to copy paste and readapt other working GET endpoints but it didn't work. I tried to log things in the getDeliveryStats func, but id didn't work. I tried to create new GET endpoints, but they had the same problem. I tried to change the operation to Put and it works, but i need to have it under Get.

Upvotes: 0

Views: 1594

Answers (1)

Dragos Lupei
Dragos Lupei

Reputation: 612

It seems that the getDeliveryStats function in your NestJS controller is not being executed when accessing the corresponding endpoint.

Imported Controller: Ensure that you have imported the DeliveryController in the appropriate module where it should be included. In NestJS, controllers need to be registered within a module to be properly recognized and associated with the corresponding routes. Make sure that the DeliveryController is included in the controllers array of the module where it should be available.

Existing Route: Check if there are any other routes defined within your application that have the same path (/delivery/stats). If there is another route with the same path defined before the getDeliveryStats route, the incoming requests might be matched to the previous route and not reach the getDeliveryStats function. In such cases, you can try reordering the routes or updating the paths to make them unique.

Upvotes: 1

Related Questions