nestjs / nest

A progressive Node.js framework for building efficient, scalable, and enterprise-grade server-side applications with TypeScript/JavaScript 🚀
https://nestjs.com
MIT License
67.77k stars 7.64k forks source link

Mongoose OverwriteModelError when using discriminator #6425

Closed 0xDones closed 3 years ago

0xDones commented 3 years ago

Bug Report

Current behavior

I'm trying to use Mongoose discriminator feature, but I'm getting this strange error even if I'm just importing the schema in the module definition. I don't know how I should approach, I'm doing the same thing described in the docs. I'm using the repository pattern, so I inject the Model in the repository and then inject the repository in the service.

Input Code

module.ts

@Module({
  imports: [
    MongooseModule.forFeatureAsync([
      { name: CornerShowerBox.name, useFactory: () => CornerShowerBoxSchema },
      { name: FrontalShowerBox.name, useFactory: () => FrontalShowerBoxSchema },
      {
        name: Product.name, useFactory: () => ProductSchema,
        discriminators: [
          { name: CornerShowerBox.name, schema: CornerShowerBoxSchema },
          { name: FrontalShowerBox.name, schema: FrontalShowerBoxSchema },
        ],
      },
    ]),
  ],
  controllers: [
    ProductController
  ],
  providers: [
    ProductService,
    ShowerBoxRepository
  ]
})
export class ProductModule { }

repository.ts

@Injectable()
export class ShowerBoxRepository {
  constructor(
    @InjectModel(CornerShowerBox.name) private cornerShowerBoxModel: Model<CornerShowerBoxDocument>,
  ) { }
...
}

Stack trace

OverwriteModelError: Cannot overwrite `CornerShowerBox` model once compiled.
    at Function.Model.discriminator (/Users/denis.policastro/dones/furnit/furnit-api/node_modules/mongoose/lib/model.js:1137:11)
    at /Users/denis.policastro/dones/furnit/furnit-api/node_modules/@nestjs/mongoose/dist/mongoose.providers.js:15:56
    at Array.forEach (<anonymous>)
    at addDiscriminators (/Users/denis.policastro/dones/furnit/furnit-api/node_modules/@nestjs/mongoose/dist/mongoose.providers.js:15:20)
    at Object.<anonymous> (/Users/denis.policastro/dones/furnit/furnit-api/node_modules/@nestjs/mongoose/dist/mongoose.providers.js:42:21)
    at Generator.next (<anonymous>)
    at fulfilled (/Users/denis.policastro/dones/furnit/furnit-api/node_modules/@nestjs/mongoose/dist/mongoose.providers.js:5:58)
    at processTicksAndRejections (internal/process/task_queues.js:97:5)

Expected behavior

Should inject the Model in the service without problems.

Possible Solution

Environment


Nest version: 7.5.4

For Tooling issues:
- Node version: v12.18.3
- Platform:  Mac

jmcdo29 commented 3 years ago

Is this related to this issues?

0xDones commented 3 years ago

@jmcdo29 Looks like it's the same problem! I was getting crazy with it 😆

Looks like there is an opened pull request with the fix (https://github.com/nestjs/mongoose/pull/692), do you have any news about that?

jmcdo29 commented 3 years ago

I mean, Kamil's asked for a reproduction and no one has provided one, so so far to me it looks blocked.

0xDones commented 3 years ago

Is this related to this issues?

@jmcdo29 The issue you pointed contains a valid repo that can be used for this purpose: https://github.com/lafeuil/nestjs-mongoose-test what do you think?

jmcdo29 commented 3 years ago

Linked them. Let's track things over there