Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ERROR [ExceptionHandler] Cannot read property '__guards__' of undefined in NestJs

I'm working on implementing a simple authentication in a Nest project.

When I add

@UseGuards(AuthGuard('local'))

to my controller I have the following Error :

ERROR [ExceptionHandler] Cannot read property '__guards__' of undefined
 at /home/cedric/Bureau/programmation/project_bank/project/node_modules/@nestjs/core/scanner.js:147:152

I followed all the Nest official documentation to do this.

My controller is

  @UseGuards(AuthGuard('local'))
  @Post('login')
  async login(@Request() req) {
    console.log(req.body.username);
    return req.body.username;
  }

and my auth.guard.ts

@Injectable()
export class LocalAuthGuard extends AuthGuard('local') {}
like image 744
Cédric Leroy Avatar asked Oct 29 '25 05:10

Cédric Leroy


2 Answers

I'd say this is a dependency version mismatch issue.

You need to ensure that @nestjs/platform-express, @nestjs/core and @nestjs/common are in the same version (I believe that only the minor slice matters).

like image 169
Micael Levi Avatar answered Oct 31 '25 18:10

Micael Levi


It really was a dependency version mismatch.

I followed this procedure to update my project :

  1. Install ncu from the npm-check-updates library:
sudo npm install -g npm-check-updates
  1. Run ncu in the project folder :
ncu
  1. Update your dependencies:
ncu -u
  1. Finally, install the updates
npm install
like image 43
Cédric Leroy Avatar answered Oct 31 '25 18:10

Cédric Leroy