How do I connect my discriminators in mongoose to my parent document? Getting errors when trying to import data How do I connect my discriminators in mongoose to my parent document? Getting errors when trying to import data mongoose mongoose

How do I connect my discriminators in mongoose to my parent document? Getting errors when trying to import data


I fixed my own issue after playing around with it some more (a lot more). Instead of calling the model and schema / discriminator and schema together, I separated them (even though that seems inconsequential from other people's 'fixes' on other sites / posts here). I also did some reorganizing just for my own sensibility for the data.

I also moved { discriminatorKey: 'kind' } to being nested in the fabricSchema (may have helped).

What seemed to have fixed it though, was after separation, I connected productSchema.path('fabrics').

The final version I used that imported data properly was:

/*const options = { discriminatorKey: 'kind', collection: 'products' }*/const reviewSchema = mongoose.Schema(  {    name: { type: String, required: true },    rating: { type: String, required: true },    comment: { type: String, required: true },  },  {    timestamps: true,  })const fabricSchema = new mongoose.Schema(  {    fabricId: { type: String, required: true },    fabricImage: { type: String },    itemImage: {      type: String,    },    availableSizes: {      type: Object,      required: true,    },  },  { discriminatorKey: 'kind' })const productSchema = new mongoose.Schema({  user: {    type: mongoose.Schema.Types.ObjectId,    required: true,    ref: 'User',  },  sku: {    type: String,    required: true,  },  name: {    type: String,    required: true,  },  image: {    type: String,    required: true,  },  category: {    type: String,    required: true,  },  fabricType: {    type: String,    required: true,  },  details: {    type: String,    required: true,  },  price: {    type: Number,    required: true,  },  wholesalePrice: {    type: Number,    required: true,  },  onSale: {    type: Boolean,    required: true,    default: false,  },  salePrice: {    type: Number,  },  reviews: [reviewSchema],  rating: {    type: Number,    required: true,    default: 0,  },  numReviews: {    type: Number,    required: true,    default: 0,  },  fabrics: [fabricSchema],})const fabricSizes = productSchema.path('fabrics')const Product = mongoose.model('Product', productSchema)// sub schemasconst standardSchema = new mongoose.Schema({  availableSizes: {    xs: { type: Number, required: true, default: 0 },    s: { type: Number, required: true, default: 0 },    m: { type: Number, required: true, default: 0 },    l: { type: Number, required: true, default: 0 },    xl: { type: Number, required: true, default: 0 },  },})const plusSizeSchema = new mongoose.Schema({  availableSizes: {    oneX: { type: Number, required: true, default: 0 },    twoX: { type: Number, required: true, default: 0 },  },})const accessorySchema = new mongoose.Schema({  availableSizes: {    os: { type: Number, required: true, default: 0 },  },})// discriminatorsconst standardProduct = fabricSizes.discriminator(  'standardProduct',  standardSchema)const plusSizeProduct = fabricSizes.discriminator(  'plusSizeProduct',  plusSizeSchema)const accessoryProduct = fabricSizes.discriminator(  'accessoryProduct',  accessorySchema)export { Product, standardProduct, plusSizeProduct, accessoryProduct }