title: NestJS with MongoDB and NestJsxAutomapper resulting in error 'cannot read property plugin of undefined'
body: <p>I am working on an API with NestJS, and because I have DTO's I am using an AutoMapper (made by @nartc and/or nestjsx), I have tried to make my example as small as I could with the <code>Foo</code> example, because I use multiple files.</p>
  <p><strong>This is my module:</strong></p>
  <pre><code>// foo.module.ts
  import { Module } from &quot;@nestjs/common&quot;;
  import { MongooseModule } from &quot;@nestjs/mongoose&quot;;
  
  import { Foo, FooSchema } from &quot;./foo.entity.ts&quot;;
  import { FooController } from &quot;./foo.controller.ts&quot;;
  import { FooService } from &quot;./foo.service.ts&quot;;
  import { FooProfile } from &quot;./foo.profile.ts&quot;;
  
  @Module({
    imports: [
      MongooseModule.forFeature([
        {
          name: Foo.name,
          schema: FooSchema,
          collection: &quot;foos&quot;,
        }
      ])
      // FooProfile &lt;-- if I uncomment this, the program will give the error (shown at the bottom of this question)
    ],
    controllers: [FooController],
    providers: [FooProivder],
  })
  export class FooModule {}
  </code></pre>
  <hr />
  <p><strong>This is my entity:</strong></p>
  <pre><code>// foo.entity.ts
  import { Schema, SchemaFactory, Prop } from &quot;@nestjs/mongoose&quot;;
  import { Document } from &quot;mongoose&quot;;
  
  @Schema()
  export class Foo extends Document { // if I remove the `extends Document` it works just fine
    @Prop({ required: true })
    name: string;
    @Prop()
    age: number
  }
  
  export const FooSchema = SchemaFactory.createForClass(Foo);
  </code></pre>
  <hr />
  <p><strong>This is my DTO:</strong></p>
  <pre><code>// foo.dto.ts
  export class FooDTO {
    name: string;
  }
  </code></pre>
  <hr />
  <p><strong>This is my controller:</strong></p>
  <pre><code>// foo.controller.ts
  import { Controller, Get } from &quot;@nestjs/common&quot;;
  import { InjectMapper, AutoMapper } from &quot;nestjsx-automapper&quot;;
  
  import { Foo } from &quot;./foo.entity&quot;;
  import { FooService } from &quot;./foo.service&quot;;
  import { FooDTO } from &quot;./dto/foo.dto&quot;;
  
  @Controller(&quot;foos&quot;)
  export class FooController {
    constructor(
      private readonly fooService: FooService
      @InjectMapper() private readonly mapper: AutoMapper
    ) {}
  
    @Get()
    async findAll() {
      const foos = await this.fooService.findAll();
      const mappedFoos = this.mapper.mapArray(foos, Foo, FooDTO);
      // ^^ this throws an error of the profile being undefined (duh)
      return mappedFoos;
    }
  }
  </code></pre>
  <hr />
  <p><strong>This is my profile:</strong></p>
  <pre><code>// foo.profile.ts
  import { Profile, ProfileBase, InjectMapper, AutoMapper } from &quot;nestjsx-automapper&quot;;
  
  import { Foo } from &quot;./foo.entity&quot;;
  import { FooDTO } from &quot;./foo.dto&quot;;
  
  @Profile()
  export class FooProfile extends ProfileBase {
    constructor(@InjectMapper() private readonly mapper: AutoMapper) {
      // I've read somewhere that the `@InjectMapper() private readonly` part isn't needed,
      // but if I exclude that, it doesn't get the mapper instance. (mapper will be undefined)
      super();
      this.mapper.createMap(Foo, FooDTO);
    }
  }
  </code></pre>
  <hr />
  <p>If I uncomment the line I highlighted in the module, it will result in the following error..</p>
  <pre class="lang-sh prettyprint-override"><code>[Nest] 11360   - 2020-08-18 15:53:06   [ExceptionHandler] Cannot read property 'plugin' of undefined +1ms
  TypeError: Cannot read property 'plugin' of undefined
      at Foo.Document.$__setSchema ($MYPATH\node_modules\mongoose\lib\document.js:2883:10)
      at new Document ($MYPATH\node_modules\mongoose\lib\document.js:82:10)
      at new Foo($MYPATH\dist\foo\foo.entity.js:15:17)
  </code></pre>
  <p>I have also referred to <a href="https://stackoverflow.com/questions/61461133/using-profiles-with-nestjsx-automapper-in-nest-js">this answer on stackoverflow</a>, but that doesn't work for me either. I have also combined that with the <a href="https://github.com/nestjsx/automapper" rel="nofollow noreferrer">documentation</a>, but with no luck.. How would I get the AutoMapper to register my profiles?</p>
  <p><strong>Update</strong></p>
  <p>The error seems to originate from the foo entity, if I remove the <code>extends Document</code> and the <code>Schema()</code>, <code>Prop({ ... })</code> from the class it works fine, it seems like I have to inject mongoose or something?</p>