[英]Nest can't resolve dependencies of the AuthService (AuthRepository, ?)
[英]NestJS Error: “Nest can't resolve dependencies of the AuthService” even everything is wired up fine
在我的 nestjs 应用程序中,我有一个User
和Auth
模块。 Auth
模块依赖于User
模块来验证用户。
现在,即使我在模块级别正确连接了我的依赖项,我仍然收到依赖项解析错误(请参阅最后一部分)。
作为参考,这是我的模块文件和相关服务。
src/user/user.module.ts
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { UserService } from './services/user.service';
import { UserController } from './user.controller';
import { UserRepository } from './user.repository';
@Module({
controllers: [UserController],
imports: [TypeOrmModule.forFeature([UserRepository])],
providers: [UserService],
exports: [UserService],
})
export class UserModule {}
src/user/user.service.ts
/** imports... **/
@Injectable()
export class UserService {
constructor(
private readonly userRepository: UserRepository
) {
}
/** some codes **/
}
src/auth/auth.module.ts
import { Module } from '@nestjs/common';
import { JwtModule } from '@nestjs/jwt';
import { PassportModule } from '@nestjs/passport';
import { UserModule } from '../user/user.module';
import { AuthController } from './auth.controller';
import { AuthService } from './services/auth.service';
import { JwtStrategy } from './strategies/jwt.strategy';
const jwtModule = JwtModule.register({
secret: "SOME_SECRET",
signOptions: { expiresIn: "1d" },
});
@Module({
imports: [jwtModule, PassportModule, UserModule],
providers: [AuthService, JwtStrategy],
controllers: [AuthController],
exports: [AuthService],
})
export class AuthModule {}
src/auth/auth.service.ts
import { Injectable } from '@nestjs/common';
import { JwtService } from '@nestjs/jwt';
import { Status } from '../../shared';
import { UserEntity, UserService } from '../../user';
@Injectable()
export class AuthService {
constructor(
private readonly jwtService: JwtService,
private readonly userService: UserService,
) {
}
}
...作为参考,我的user
模块文件夹中有一个桶文件。
src/用户/index.ts
export * from './user.entity';
export * from './user.interface';
export * from './services/user.service';
app.module.ts
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { AuthModule } from './auth/auth.module';
import { CoreModule } from './core/core.module';
import { ConfigService } from './core/services/config.service';
import { SharedModule } from './shared/shared.module';
import { UserModule } from './user/user.module';
/**
* ORM configuration
*/
const typeORM = TypeOrmModule.forRoot({
/** typeorm configuration **/
});
@Module({
imports: [
typeORM,
CoreModule,
SharedModule,
UserModule,
AuthModule,
],
})
export class AppModule {}
如您所见,我在 UserModule 中导出了我的UserService
UserModule
。 UserService
也用@Injectable
装饰器装饰。 最后,请注意我在AuthModule
的导入部分中导入了UserModule
。
但是当我尝试运行该应用程序时,会出现以下错误:
Nest can't resolve dependencies of the AuthService (JwtService, ?). Please make sure that the argument dependency at index [1] is available in the AuthModule context.
鉴于上述代码文件,nest 应该没有理由无法在AuthService
中找到UserService
。 我错过了什么吗?
正如@nerdy beast 在评论中指出的那样,这个问题实际上与从桶文件中导入文件有关。 就我而言,它与typeorm
相关——如此处所述https://github.com/typeorm/typeorm/issues/420#issuecomment-307219380 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.