nest: [ExceptionHandler] Nest can't resolve dependencies of the UserController (?, +)...
I’m submitting a…
[ ] Regression
[X] Bug report
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.
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.
Minimal reproduction of the problem with instructions
application.module.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 {}
user.module.ts
import { Module } from "@nestjs/common";
import { UserController } from "controllers/user.controller";
@Module({
controllers: [
UserController,
],
})
export class UserModule {}
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
Others:
IDE: VSC
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 1
- Comments: 18 (4 by maintainers)
AuthModuleshould provideAuthService, notApplicationModulebecauseAuthModuleimports JwtModule, which exposes JwtService. If you importedJwtModuleinApplicationModule, it would work, but that wouldn’t be good separation of concerns@johnbiundo I realize that this is not really the place to discuss this (i should open an issue) but since i was browsing this issue and you just asked, so here are two quick personal suggestions about the docs:
Important note: This post is only for those who has similar problem, but not exactly the same like reporter. Related to typing.
Previously (when error occurred), I had in
users.controller.ts:What was helpful for me:
For those who might be interested, I ran into the same problem and was able to solve it by including
TypeOrmModule.forFeature()in the imports section ofauth.module.tsfile.Check https://docs.nestjs.com/techniques/database - you need to add
TypeOrmModule.forFeature([UserEntity])to your module imports@mkelandis your
UserModulehas to exportTypeOrmModule. Then,TypeOrmModule.forFeature([User])won’t be requiredYou need to provide
UserServicein a module. AddingUserServiceto providers inapplication.module.tsshould work