Mongoose discriminators undefined

Viewed 18

I have a Schema with discriminators, but when I run mySchema.discriminators, it's returning undefined, even though when I console.log the schema, it has the discriminators in it. Is this expected? How do I go about getting the list of discriminators?

My Schema

const mongoose = require('mongoose');

const Schema = mongoose.Schema;
const options = { discriminatorKey: 'kind', collection: 'properties' };

const PropertySchema = new Schema ({
    name: {
        type: String,
        required: true
    },
    description: String
}, options);

const Property = mongoose.model('Property', PropertySchema);

const Integer = Property.discriminator('Integer', new Schema({
    bounds: {
        max: { type: 'Number', default: -Infinity },
        min: { type: 'Number', default: Infinity }
    }
}, options))

module.exports = {Property: Property, Integer: Integer}

My controller

const PropertyModel = require('../models/property');
module.exports.index = async (req, res) => {
    // some await function 
    console.log(PropertyModel);
    console.log(PropertyModel.discriminators)
    res.render('properties/index');
}

Console Output

{
  Property: Model { Property },
  Integer: Model { Integer },
  Number: Model { Number },
  Money: Model { Money },
  RadioSelect: Model { RadioSelect },
  Checkbox: Model { Checkbox },
  TieredSelect: Model { TieredSelection },
  SingleSelect: Model { SingleSelect },
  MultiSelect: Model { MultiSelect },
  SingleUser: Model { SingleUser },
  MultiUser: Model { MultiUser },
  SingleGroup: Model { SingleGroup },
  MultiGroup: Model { MultiGroup },
  Date: Model { Date },
  DateTime: Model { DateTime },
  Labels: Model { Label },
  ShortText: Model { ShortText },
  LongText: Model { LongText }
}
undefined
1 Answers

I was able to access the discriminator names using Object.keys(PropertyModel).

Related