DEV Community

Discussion on: Getting started with Mongoose discriminators in Express.js

Collapse
 
the_hme profile image
Brenda 🐱 • Edited

Update below:

Thanks for starting this post. I am using Mongoose 5.3 and I'm having issues with making changes to my existing schema.

I don't have each model in different files, they are all in the same file and then get exported as part of a single app schema, so how would I access Base within the same file. I tried something like this:

const Base = new mongoose.Schema({
      title: { type: String, required: true },
      date_added: { type: Date, required: true },
      redo: { type: Boolean, required: false },
    }, baseOptions,
  );

const BookSchema = new mongoose.Schema({
    author: { type: String, required: true },
  });

var Book = Base.discriminator('Book', BookSchema);
...
this.Base = mongoose.model('Base', Base);
this.Book = mongoose.model('Book', Book);

However, when I try to load my app, it complains with error:

Base.discriminator is not a function

I tried upgrading mongoose to version 5.4.2 and I am still getting this error.

UPDATE: My issue seems related to the fact that I need to work on a deeply embedded doc and this array of objects is what needs to be able to inherit from the base class. Example, person object with embedded belongings array, which contains a media array, where the movies, books, etc. are store and these media items need to share a schema

Collapse
 
rifaldikn profile image
Rifaldikn

Same with me, did u solve it?