错误 [ExceptionHandler] 无法读取 NestJs 中未定义的属性“__guards__”

Céd*_*roy 2 authentication typescript passport.js nestjs

我正在努力在 Nest 项目中实现简单的身份验证。

当我添加

@UseGuards(AuthGuard('local'))
Run Code Online (Sandbox Code Playgroud)

我的控制器出现以下错误:

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

我遵循所有 Nest 官方文档来执行此操作。

我的控制器是

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

和我的 auth.guard.ts

@Injectable()
export class LocalAuthGuard extends AuthGuard('local') {}
Run Code Online (Sandbox Code Playgroud)

Mic*_*evi 8

我想说这是一个依赖版本不匹配的问题。

您需要确保@nestjs/platform-express@nestjs/core@nestjs/common处于同一版本(我相信只有小部分很重要)。