NestJS无法解析AuthServices的依赖项 [英] NestJS can't resolve dependencies of the AuthServices
问题描述
在第一个JWT_MODULE_OPTION问题之后,返回我以为我已经解决了这个老问题.原来,当我解决"旧问题时,请使用JWT创建新问题.
After first problem with JWT_MODULE_OPTION, back to old problem who I thought I was fixed. It turned out that when I "fix" old problem create the new with JWT.
所以再次无法编译:
嵌套不能解析AuthService的依赖项(?,RoleRepository,JwtService).请确保在AppModule上下文中索引[0]处的参数可用. + 25ms
这真的很奇怪,因为这种方式可以在我的另一个项目上工作,并且无法理解我哪里写错了.这是 auth.service.ts :
It's really strange, because this way work on another my project and can't understand where I'm wrong. Here is the auth.service.ts:
@Injectable()
export class AuthService {
constructor(
@InjectRepository(User) private readonly userRepo: Repository<User>,
@InjectRepository(Role) private readonly rolesRepo: Repository<Role>,
private readonly jwtService: JwtService,
) { }
它获得了角色和jwtService
,但是问题出在User
上,路径正确.这是 app.module.ts :
It get role and jwtService
but the problem is with User
, the path is correct. Here is app.module.ts:
@Module({
imports: [
TypeOrmModule.forRootAsync({
imports: [ConfigModule, AuthModule],
inject: [ConfigService],
useFactory: async (configService: ConfigService) => ({
type: configService.dbType as any,
host: configService.dbHost,
port: configService.dbPort,
username: configService.dbUsername,
password: configService.dbPassword,
database: configService.dbName,
entities: ['./src/data/entities/*.ts'],
}),
}),
],
controllers: [AppController, AuthController],
providers: [AuthService],
})
export class AppModule { }
对于控制器&具有相同的编译错误;提供者和不明白怎么了...
Have the same compile error for controllers & providers & can't understand what is wrong...
推荐答案
您可能缺少TypeOrmModule.forFeature([User])
导入.通常,所有实体都将导入到专用功能模块中.如果只有一个模块(即AppModule
),则除了forRoot
导入之外,还需要在其中放置forFeature
导入.
You might be missing the TypeOrmModule.forFeature([User])
import. Typically, all entities are imported in dedicated feature modules. If you only have one module (i.e. AppModule
) you need to put the forFeature
import there in addition to the forRoot
import.
@Module({
imports: [
TypeOrmModule.forRootAsync({...}),
TypeOrmModule.forFeature([User, Role]),
],
这篇关于NestJS无法解析AuthServices的依赖项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!