0
votes

I'm trying to use the JwtService from nestjs to decode an access_token in a controller, but I don't know how to inject this service in the constructor.

Here is my code:

import { JwtService } from '@nestjs/jwt';
import { Controller, Get, Post, Body, Req } from '@nestjs/common';
@Controller('event')
export class EventController {
    constructor(
        private readonly jwtService: JwtService) {}

@Get('/questions')
    async getQuestionsAsync(): Promise<Question[]> {
    const decodedToken = this.jwtService.decode(accessToken);
    ...
}

I'm getting this error:

Error: Nest can't resolve dependencies of the EventController (EventService, ?). Please make sure that the argument JwtService at index [1] is available in the EventModule context.

I have already tried to add @Inject(JwtService) as an attribute to the private variable, but the error is the same.

So, how do I correctly inject the JwtService in the controller ?

1

1 Answers

2
votes

You need to make sure that JwtModule is added to the imports array of the EventModule or if it is configured elsewhere that it is exported from the module it is configured in and that the configuring module is imported into EventModule.