【发布时间】:2018-12-27 07:55:40
【问题描述】:
我有一个 AuthGuard,负责检查控制器中的 JWT 令牌。我想在控制器中使用这个 Guard 来检查身份验证。我有这个错误:
Nest 无法解析 AuthGuard (?, +) 的依赖关系。请确保索引 [0] 处的参数在当前上下文中可用。
TestController.ts
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();
}
}
AuthGuard.ts
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;
}
}
}
【问题讨论】:
-
你能包含你的模块吗?
标签: node.js typescript nestjs