title: NestJS: How to inject a service into a provider from the same module?
body: <p>Is it possible to inject a service into a factory provider from the <strong>same module</strong>? I have a service <code>AppService</code> that I would like to use in this factory provider below.</p>
  
  <p>Here is my code:</p>
  
  <p><code>app.module.ts</code></p>
  
  <pre><code>@Module({
    imports: [
      MongooseModule.forFeatureAsync([
          {
              name: 'List',
              imports: [AnotherModule],
              useFactory: (anotherService: AnotherService) =&gt; {
                  const schema = ListSchema;
                  schema.pre('save', function() {
                      // Validate
                  })
                  return schema;
              },
              inject: [AnotherService],
          },
    ],
    providers: [AppService],
    exports: [AppService],
  })
  export class AppModule {}
  </code></pre>
  
  <p>I want to be able to do something like this:</p>
  
  <p><code>app.module.ts</code></p>
  
  <pre><code>@Module({
    imports: [
      MongooseModule.forFeatureAsync([
          {
              name: 'List',
              imports: [AnotherModule, AppModule],
              useFactory: (anotherService: AnotherService, appService: AppService) =&gt; {
                  const schema = ListSchema;
                  schema.pre('save', function() {
                      // Validate
                  })
                  return schema;
              },
              inject: [AnotherService, AppService],
          },
    ],
    providers: [AppService],
    exports: [AppService],
  })
  export class AppModule {}
  </code></pre>
  
  <p>This does not work. Nest fails to initialize all dependencies and the application does not run.</p>
  
  <p>Is it even possible to do something like this?</p>
  
  <p>How can I inject a service from the same module into this factory provider? One solution would be to move that service to a different module and then inject that service. However, I would like to avoid that if possible.</p>