[英]NestJS can't resolve dependencies of the UserController
嘗試在UserController.ts中使用UserService.ts和AuthService.ts ,但出現以下錯誤: [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.
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(); } ...
修正錯誤?
巢狀版本: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 {}
我一直認為import
某個模塊就足夠了,但是根據我的經驗,我總是不得不在components
部分中聲明它。
我還意識到,您忘記在組件的模塊中聲明UserService
了
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.