How to Create and Use Enum in Mongoose

Issue

I am trying to create and use an enum type in Mongoose. I checked it out, but I’m not getting the proper result. I’m using enum in my program as follows:

My schema is:

var RequirementSchema = new mongooseSchema({
   status: {
        type: String,
        enum : ['NEW','STATUS'],
        default: 'NEW'
    },
})

But I am little bit confused here, how can I put the value of an enum like in Java NEW("new"). How can I save an enum in to the database according to it’s enumerable values. I am using it in express node.js.

Thanks

Solution

The enums here are basically String objects. Change the enum line to enum: ['NEW', 'STATUS'] instead. You have a typo there with your quotation marks.

Answered By – Mindstormer619

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published