繁体   English   中英

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

用户模块.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 {}

用户服务.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,但它已经存在,我试图删除每个用户 model 或 AuthModule 并将它们混合到所有内容中,但它仍然不起作用,我知道我必须将 UserService 导出到 AuthModule ,但找不到正确的方法。

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

仔细检查您的模块导入:

@Module({
  imports: [
    MongooseModule.forFeature([
       { name: 'User', schema: UserSchema }
    ]),
    ...
  ],
  ...
})
export class XModule {}

并寻找愚蠢的错误,因为即使您传递模式和模式名称而不是彼此,也不会出现类型错误! 一般的Nest can't resolve dependencies将是您在这里可能出现的许多错误所获得的全部......

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM