typescript - NestJS 无法解析 UserController 的依赖关系

标签 typescript nestjs

当前行为

尝试在我的 UserController.ts 中使用我的 UserService.tsAuthService.ts,但出现以下错误: [ExceptionHandler] Nest 无法解析 UserController (?, +) 的依赖关系。请确保索引 [0] 处的参数在当前上下文中可用。

通过说明最小化问题重现

应用程序模块.ts
import { Module } from "@nestjs/common";
import { ApplicationController } from "controllers/application.controller";
import { ApplicationService } from "services/application.service";
import { AuthModule } from "./auth.module";
import { UserModule } from "./user.module";

@Module({
  imports: [
    AuthModule,
    UserModule,
  ],
  controllers: [
    ApplicationController,
  ],
  providers: [
    ApplicationService,
  ],
})
export class ApplicationModule {}
用户模块.ts
import { Module } from "@nestjs/common";
import { UserController } from "controllers/user.controller";

@Module({
  controllers: [
    UserController,
  ],
})
export class UserModule {}
用户服务.ts
import { Injectable } from "@nestjs/common";
import { InjectRepository } from "@nestjs/typeorm";
import { Repository } from "typeorm";
import { UserEntity } from "entities/user.entity";

@Injectable()
export class UserService {
  constructor(
    @InjectRepository(UserEntity)
    private readonly repository: Repository<UserEntity>,
  ) {}

  async findAll(): Promise<UserEntity[]> {
    return await this.repository.find();
  }
}
auth.module.ts
import { Module } from "@nestjs/common";
import { JwtModule } from "@nestjs/jwt";
import { AuthService } from "services/auth.service";

@Module({
  imports: [
    JwtModule.register({
      secretOrPrivateKey: "key12345",
    }),
  ],
})
export class AuthModule {}
auth.service.ts
import { Injectable } from "@nestjs/common";
import { JwtService } from "@nestjs/jwt";
import { TokenJwtInterface } from "interfaces/token-jwt.interface";

@Injectable()
export class AuthService {
  private tokenType;

  constructor(private readonly jwtService: JwtService) {
    this.tokenType = "bearer";
  }

  public generateTokenJwt(
    payload: object,
    expiresIn: number,
  ): TokenJwtInterface {
    const accessToken = this.jwtService.sign(payload);

    return {
      access_token: accessToken,
      token_type: this.tokenType,
      refresh_token: "",
      expires_in: expiresIn,
    };
  }
}
用户 Controller .ts
import {
  Get,
  Controller,
  Post,
  Body,
  HttpCode,
  HttpStatus,
} from "@nestjs/common";
import { UserService } from "services/user.service";
import { UserEntity } from "entities/user.entity";
import * as bcrypt from "bcryptjs";
import { AuthService } from "services/auth.service";

@Controller("/users")
export class UserController {
  constructor(
    private readonly userService: UserService,
    private readonly authService: AuthService,
  ) {}

  @Get()
  async root(): Promise<UserEntity[]> {
    return await this.userService.findAll();
  }
...

改变行为的动机/用例是什么?

修正错误?

环境

巢版本:5.1.0 对于工具问题: - 节点版本:v8.11.3 - 平台:Ubuntu - IDE:VSC

最佳答案

您的 AuthService 必须是您的 UserModule 的一部分

import { Module } from "@nestjs/common";
import { UserController } from "controllers/user.controller";

@Module({
   controllers: [
     UserController,
   ],
   components: [
     UserService,
     AuthService
   ],
   imports: [
     AuthModule 
   ]
})
export class UserModule {}

我一直认为导入某些模块就足够了,但根据我的经验,我总是必须在组件部分中声明它。

我还意识到您忘记在组件模块中声明 UserService

关于typescript - NestJS 无法解析 UserController 的依赖关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51386001/

相关文章:

javascript - Typescript/Angular 7 : How can I iterate over Object matching my Interface with Object. 键?

angular - 参数类型 'action' 和 'action' 在 Angular ngrx 存储项目中不兼容

swagger - SwaggerUI 中的 NestJS 按字母顺序排列端点

nestjs - 未知数据库 - [TypeOrmModule] 无法连接到数据库

typescript - 错误 : Schema must contain uniquely named types but contains multiple types named "n"

node.js - Jest/Ts-Jest/Babel : Debug Failure. 错误表达式:输出生成失败

javascript - 是否可以声明自己的 typescript 模块并像 npm 包一样导入它们

node.js - "EACCESS: permission denied"使用 Angular 8 和 Nodejs 从 ubuntu 中的服务器使用 POST 方法下载 xlsx 时出现问题

javascript - 将枚举从 Typescript 转换为 JavaScript 时出错

typescript - 如何使用 Nestjs Pipes 和 FileTypeValidator 验证文件类型