0

Using "mongoose": "4.9.8", I want to define a type for consistency and reuse. I know the concept, but I'm a little stuck on the syntax, For instance, say my mongoose schema looks like this

const thingsSchema = new Schema({
    thing: {
        type: String
        ,required: false
    }   
    ,location: {
        city: {
            type: String
            ,required: false
        }
        ,state: {
            type: String
            ,required: false
        }
        ,country: {
            type: String
            ,required: false
        }
    }   
});

what I really want to do is use "Location" model/type over again elsewhere, and have location not be required...like this:

const thingsSchema = new Schema({
    thing: {
        type: String
        ,required: false
    }   
    ,location: {
        type: Location
        ,required: false
    }   
});

I think it's done something like this - but Im unsure.

const Location: {
        city: {
            type: String
            ,required: false
        }
        ,state: {
            type: String
            ,required: false
        }
        ,country: {
            type: String
            ,required: false
        }
    };
j-p
  • 3,698
  • 9
  • 50
  • 93
  • Possible duplicate of [Nested objects in mongoose schemas](https://stackoverflow.com/questions/39596625/nested-objects-in-mongoose-schemas) – FisNaN Mar 21 '18 at 03:45
  • Possible, yes, the second answer there makes a go at it - and it may be right, but it is poorly explained. Thx for the link. – j-p Mar 21 '18 at 04:28

0 Answers0