简体   繁体   中英

Nest can't resolve dependencies of the UserService

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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