I'm using mongoose to interact with mongoDB in a rest API, and while using discriminators, I get this error :
Discriminator with name "serviceMember" already exists
know that with this code everything is fine :
const serviceManagerSchema = new Schema({
    serviceId : {
    	type : Schema.Types.ObjectId,
    	ref : 'service'
    },
    serviceMembers : [{
    	type : Schema.Types.ObjectId,
    	ref : 'serviceMember'
    }] 
},options)
const ServiceManager = Employee.discriminator('serviceManager',serviceManagerSchema)
but this doesn't work
const serviceMemberSchema = new Schema({
 
 },options)
const ServiceMember = Employee.discriminator('serviceMember',serviceMemberSchema)
module.exports = ServiceMember
I have tried to replace the "serviceMember" name with another name that i'm sure i have never used it, but still the same error
Help me please, thank you in advance
I know this question has cobwebs on it, but in case anyone else ever comes here looking for a solution to this problem here's how I solved it. In my case I was getting this error due to using Mongoose with Next.js - everytime Next hot reloaded my model module (in my case, the base model is called 'User'), all of the discriminators would collide.
To solve the issue, I had to delete the discriminator model (because Mongoose creates a full-fledged model for each discriminator) and then also delete the discriminator from the User model:
// Patient handler
if (mongoose.modelNames().includes('patient')) {
  log.trace('Deleting patient model');
  mongoose.deleteModel('patient');
}
if (User.discriminators && 'patient' in User.discriminators) {
  log.trace('Deleting patient discriminator');
  delete User.discriminators.patient;
}
const Patient = User.discriminator(
  'patient',
  new mongoose.Schema(
    {
      healthCard: { type: String, unique: true },
      homePhone: { type: String },
      birthDate: { type: Date, required: true },
    },
    options
  )
);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With