Nest无法解析UserService的依存关系

时间:2019-11-11 17:51:51

标签: javascript nest nestjs

我遇到此错误

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 ,但找不到正确的方法。

1 个答案:

答案 0 :(得分:1)

您正在<script> if (!window.deferAfterjQueryLoaded) { window.deferAfterjQueryLoaded = []; //delete Object.defineProperty; //add this if you want to check the support if (typeof (Object.defineProperty) === "function") { Object.defineProperty(window, "jQuery", { set: function (value) { window.setTimeout(function () { $.each(window.deferAfterjQueryLoaded, function (index, fn) { fn(); }); }, 0); Object.defineProperty(window, "jQuery", {value: value}); }, configurable: true }); } else { nIntervId = window.setInterval(function () { if (typeof (jQuery) !== "undefined") { $.each(window.deferAfterjQueryLoaded, function (index, fn) { fn(); }); clearInterval(nIntervId); } }, 10); } } window.deferAfterjQueryLoaded.push(function () { (function ($) { console.log("After jquery is loaded"); })(jQuery); }); </script> <script> window.deferAfterjQueryLoaded.push(function () { (function ($) { console.log("After jquery is do something else"); })(jQuery); }); </script> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>中提供UserService。但是,它只能位于您的AuthModule中。在UserModule中,AuthModule是未知的。