简体   繁体   中英

NestJS can't resolve dependencies of the UserController

Current behavior

Trying to use my UserService.ts and AuthService.ts in my UserController.ts , but I get the following error: [ExceptionHandler] Nest can't resolve dependencies of the UserController (?, +). Please make sure that the argument at index [0] is available in the current context. [ExceptionHandler] Nest can't resolve dependencies of the UserController (?, +). Please make sure that the argument at index [0] is available in the current context.

Minimal reproduction of the problem with instructions

application.module.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();
  }
}
user.module.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();
  }
...
user.service.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, }; } } 
user.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(); } ... 

What is the motivation / use case for changing the behavior?

Bugfix?

Environment

Nest version: 5.1.0 For Tooling issues: - Node version: v8.11.3 - Platform: Ubuntu - IDE: VSC

Your AuthService must be part of your UserModule

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

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

I always thought that import ing some module would be enough, but in my experience I always had to declare it in the components section.

I also realized that you forgot to declare your UserService in your components' module

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM