Prabjot Singh Prabjot Singh - 4 years ago 187
Node.js Question

How to Create and Use Enum in Mongoose

I am trying to create and use an

enum
object 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.

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download