最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Nest can't resolve dependencies of the UserService - Stack Overflow

programmeradmin2浏览0评论

I´m having this error

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

I tried everything and can't find the issue, everything seems correct, i even checked every google page results to try to find it but i'm stuck. The error tells me that i need to import UserModel into AuthModule, but it's already there, i tried to delete every single user model, or the AuthModule and mix them into everything and it still doesnt work, i know i have to export UserService to AuthModule, but can't find the correct way.

I´m having this error

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

I tried everything and can't find the issue, everything seems correct, i even checked every google page results to try to find it but i'm stuck. The error tells me that i need to import UserModel into AuthModule, but it's already there, i tried to delete every single user model, or the AuthModule and mix them into everything and it still doesnt work, i know i have to export UserService to AuthModule, but can't find the correct way.

Share Improve this question edited Nov 11, 2019 at 18:01 Jose CastilLo Stronghold asked Nov 11, 2019 at 17:51 Jose CastilLo StrongholdJose CastilLo Stronghold 691 gold badge1 silver badge10 bronze badges 4
  • 1 Can you post your UserService class ? – Nicolas Commented Nov 11, 2019 at 17:57
  • @Nicolas i updated my question with part of the file, the functions are just on crud operations. – Jose CastilLo Stronghold Commented Nov 11, 2019 at 18:02
  • What's the constructor of your AuthService look like? Standard DI syntax: constructor(private readonly userService: UserService, private readonly settingsService: SettingsService){}? – Jay McDoniel Commented Nov 11, 2019 at 18:12
  • That's right, i have it just like you posted. – Jose CastilLo Stronghold Commented Nov 11, 2019 at 18:31
Add a ment  | 

2 Answers 2

Reset to default 4

You are providing the UserService in your AuthModule. However, it should only be in your UserModule. In the AuthModule, the UserModel is unknown.

Double-check your module imports:

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

And look for silly mistakes! because even if you pass schema and schema name instead of each other, there will be no type errors! The general Nest can't resolve dependencies will be all you get for so many mistakes that are probable here...

发布评论

评论列表(0)

  1. 暂无评论