【发布时间】:2018-12-25 10:16:53
【问题描述】:
当前行为
尝试在我的 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.
用指令最小化重现问题
应用程序.module.tsimport { 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,
};
}
}
用户控制器.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
【问题讨论】:
-
从概念上讲,我遇到了与 NestJS 依赖项相同的问题,它与 HttpService 有关。这篇文章有解决方案mydaytodo.com/…
标签: typescript nestjs