Nest 无法解析 UserService 的依赖关系
Nest can't resolve dependencies of the UserService
我遇到了这个错误
Nest can't resolve dependencies of the UserService (?, SettingsService). Please make sure that the argument UserModel at index [0] is available in the AuthModule context.
Potential solutions:
- If UserModel is a provider, is it part of the current AuthModule?
- If UserModel is exported from a separate @Module, is that module imported within AuthModule?
@Module({
imports: [ /* the Module containing UserModel */ ]
})
auth.module.ts
@Module({
imports: [
PassportModule.register({ defaultStrategy: 'jwt' }),
JwtModule.register({
secretOrPrivateKey: config.auth.secret,
signOptions: {
expiresIn: config.auth.expiresIn,
},
}),
UserModule,
SettingsModule,
],
controllers: [AuthController],
providers: [
AuthService,
JwtStrategy,
LocalStrategy,
UserService,
SettingsService,
Logger,
... other services,
],
exports: [PassportModule, AuthService],
})
export class AuthModule {}
user.module.ts
@Module({
imports: [
MongooseModule.forFeature([{ name: 'User', schema: UserSchema }]),
SettingsModule,
],
controllers: [UserController],
providers: [UserService],
exports: [UserService],
})
export class UserModule {}
app.module.ts
@Module({
imports: [
AuthModule,
UserModule,
SettingsModule,
MongooseModule.forRoot(config.db.url),
WinstonModule.forRoot({
level: config.logger.debug.level,
}),
],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}
user.service.ts
@Injectable()
export class UserService {
constructor(@InjectModel('User') private readonly userModel: Model<User>,
private readonly settingsService: SettingsService) {}
public async create(user: any): Promise<UserDto> {
...
}
我尝试了所有方法但找不到问题,一切似乎都是正确的,我什至检查了每个 google 页面结果以试图找到它,但我被卡住了。
该错误告诉我我需要将 UserModel 导入 AuthModule,但它已经存在,我试图删除每个用户模型或 AuthModule 并将它们混合到所有内容中,但它仍然不起作用,我知道我必须将 UserService 导出到 AuthModule , 但找不到正确的方法。
您正在 AuthModule
中提供 UserService
。但是,它应该只在您的 UserModule
中。在AuthModule
中,UserModel
未知。
我遇到了这个错误
Nest can't resolve dependencies of the UserService (?, SettingsService). Please make sure that the argument UserModel at index [0] is available in the AuthModule context.
Potential solutions:
- If UserModel is a provider, is it part of the current AuthModule?
- If UserModel is exported from a separate @Module, is that module imported within AuthModule?
@Module({
imports: [ /* the Module containing UserModel */ ]
})
auth.module.ts
@Module({
imports: [
PassportModule.register({ defaultStrategy: 'jwt' }),
JwtModule.register({
secretOrPrivateKey: config.auth.secret,
signOptions: {
expiresIn: config.auth.expiresIn,
},
}),
UserModule,
SettingsModule,
],
controllers: [AuthController],
providers: [
AuthService,
JwtStrategy,
LocalStrategy,
UserService,
SettingsService,
Logger,
... other services,
],
exports: [PassportModule, AuthService],
})
export class AuthModule {}
user.module.ts
@Module({
imports: [
MongooseModule.forFeature([{ name: 'User', schema: UserSchema }]),
SettingsModule,
],
controllers: [UserController],
providers: [UserService],
exports: [UserService],
})
export class UserModule {}
app.module.ts
@Module({
imports: [
AuthModule,
UserModule,
SettingsModule,
MongooseModule.forRoot(config.db.url),
WinstonModule.forRoot({
level: config.logger.debug.level,
}),
],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}
user.service.ts
@Injectable()
export class UserService {
constructor(@InjectModel('User') private readonly userModel: Model<User>,
private readonly settingsService: SettingsService) {}
public async create(user: any): Promise<UserDto> {
...
}
我尝试了所有方法但找不到问题,一切似乎都是正确的,我什至检查了每个 google 页面结果以试图找到它,但我被卡住了。 该错误告诉我我需要将 UserModel 导入 AuthModule,但它已经存在,我试图删除每个用户模型或 AuthModule 并将它们混合到所有内容中,但它仍然不起作用,我知道我必须将 UserService 导出到 AuthModule , 但找不到正确的方法。
您正在 AuthModule
中提供 UserService
。但是,它应该只在您的 UserModule
中。在AuthModule
中,UserModel
未知。