Node.js 巢罐';t解析AuthGuard(保护装饰器)的依赖关系

Node.js 巢罐';t解析AuthGuard(保护装饰器)的依赖关系,node.js,typescript,nestjs,Node.js,Typescript,Nestjs,我有一个AuthGuard在控制器中检查JWT令牌。我想在控制器中使用此防护来检查身份验证。我发现了这个错误: 嵌套无法解析AuthGuard(?,+)的依赖项。请确保索引[0]处的参数在当前上下文中可用 TestController.ts 导入{ 控制器, 邮递 身体 HttpCode, HttpStatus, 使用拦截器, 卫兵, }来自“@nestjs/common”; 从“Services/TestService”导入{TestService}; 从“Dtos/CreateTestDto

我有一个AuthGuard在控制器中检查JWT令牌。我想在控制器中使用此防护来检查身份验证。我发现了这个错误:

嵌套无法解析AuthGuard(?,+)的依赖项。请确保索引[0]处的参数在当前上下文中可用

TestController.ts
导入{
控制器,
邮递
身体
HttpCode,
HttpStatus,
使用拦截器,
卫兵,
}来自“@nestjs/common”;
从“Services/TestService”导入{TestService};
从“Dtos/CreateTestDto”导入{CreateTestDto};
从“@nestjs/swagger”导入{ApiConsumes,apiproducts};
从“Guards/AuthGuard”导入{AuthGuard};
@控制器(“/tests”)
@UseGuards(AuthGuard)
导出类TestController{
建造师(
私有只读testService:testService,
) {}
@Post(“/create”)
@HttpCode(HttpStatus.OK)
@ApiConsumes(“应用程序/json”)
@API(“应用程序/json”)
异步创建(@Body()createTestDto:createTestDto):承诺{
//this.testService.blabla();
}
}
AuthGuard.ts
import{CanActivate,ExecutionContext,Injectable}来自“@nestjs/common”;
从“服务/AuthService”导入{AuthService};
从“Services/UserService”导入{UserService};
@可注射()
导出类AuthGuard实现了CanActivate{
建造师(
专用只读authService:authService,
私有只读用户服务:用户服务,
) {}
async canActivate(dataOrRequest,上下文:ExecutionContext):承诺{
试一试{
//代码在这里
返回true;
}捕获(e){
返回false;
}
}
}
AuthService
(无法解析的依赖项)必须在包含使用防护的控制器的作用域中可用

这是什么意思?

在加载控制器的模块的
提供程序中包括
AuthService

e、 g

编辑-忘了提到另一种干净的方式(可能更干净,取决于上下文)是导入提供(
导出
)服务的模块

e、 g

AuthService
(无法解析的依赖项)必须在包含使用保护的控制器的作用域中可用

这是什么意思?

在加载控制器的模块的
提供程序中包括
AuthService

e、 g

编辑-忘了提到另一种干净的方式(可能更干净,取决于上下文)是导入提供(
导出
)服务的模块

e、 g


你能包括你的模块吗?
import {
  Controller,
  Post,
  Body,
  HttpCode,
  HttpStatus,
  UseInterceptors,
  UseGuards,
} from "@nestjs/common";
import { TestService } from "Services/TestService";
import { CreateTestDto } from "Dtos/CreateTestDto";
import { ApiConsumes, ApiProduces } from "@nestjs/swagger";
import { AuthGuard } from "Guards/AuthGuard";

@Controller("/tests")
@UseGuards(AuthGuard)
export class TestController {
  constructor(
    private readonly testService: TestService,
  ) {}

  @Post("/create")
  @HttpCode(HttpStatus.OK)
  @ApiConsumes("application/json")
  @ApiProduces("application/json")
  async create(@Body() createTestDto: CreateTestDto): Promise<void> {
    // this.testService.blabla();
  }
}
import { CanActivate, ExecutionContext, Injectable } from "@nestjs/common";
import { AuthService } from "Services/AuthService";
import { UserService } from "Services/UserService";

@Injectable()
export class AuthGuard implements CanActivate {
    constructor(
        private readonly authService: AuthService,
        private readonly userService: UserService,
    ) {}

    async canActivate(dataOrRequest, context: ExecutionContext): Promise<boolean> {
        try {
            // code is here
            return true;
        } catch (e) {
            return false;
        }
    }
}
@Module({
  controllers: [TestController],
  providers: [AuthService, TestService, UserService],
})
export class YourModule {}
@Module({
  providers: [AuthService],
  exports: [AuthService],
})
export class AuthModule {}

@Module({
  imports: [AuthModule],
  controllers: [TestController],
  providers: [TestService, UserService],
})
export class YourModule {}