gpt4 book ai didi

javascript - Nest 无法解析 UserService 的依赖关系

转载 作者:行者123 更新时间:2023-12-05 02:58:36 24 4
gpt4 key购买 nike

我有这个错误

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> {
...
}

我尝试了所有方法但找不到问题,一切似乎都是正确的,我什至检查了每个谷歌页面结果以试图找到它,但我被卡住了。该错误告诉我我需要将 UserModel 导入 AuthModule,但它已经存在,我试图删除每个用户模型或 AuthModule 并将它们混合到所有内容中,但它仍然不起作用,我知道我必须将 UserService 导出到 AuthModule , 但找不到正确的方法。

最佳答案

您在 AuthModule 中提供了 UserService。但是,它应该只在您的 UserModule 中。在 AuthModule 中,UserModel 是未知的。

关于javascript - Nest 无法解析 UserService 的依赖关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58806309/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com