Mongoose: Error: `enum` can only be set on an array of strings, not Mixed

Created on 5 Mar 2018  路  6Comments  路  Source: Automattic/mongoose

The following line in the definition of a collection crashes:
status: { type: String, enum: Object.values(ENUMS.STATUS), default: ENUMS.STATUS.INACTIVE }

ENUMS.STATUS = {
  ACTIVE: 'active',
  INACTIVE: 'inactive',
  DELETED: 'deleted',
  DISABLED: 'disabled'
};

We are using the node 9.7.1 and Mongoose 5.0.8. With 5.0.5 works fine but with 5.0.6+ it throws:
Error: enum can only be set on an array of strings, not Mixed

enhancement

Most helpful comment

The issue is permissions: { type: Array, default: [], enum: PERMISSIONS }. enum is a no-op on arrays of non-strings, and before 5.0.6 setting enum on an array did nothing. You should do:

permissions: { type: [String], enum: PERMISSIONS }

The default: [] is also unnecessary, mongoose creates an empty array by default.

All 6 comments

@alejandromagnorsky that error only happens with arrays, can you paste your whole schema please?

Here is the full schema:

{
  _id: { type: ObjectId, auto: true },
  first_name: { type: String, required: true },
  last_name: { type: String, required: true },
  email: { type: String, lowercase: true },
  username: { type: String, lowercase: true },
  mobile: { type: String },
  role: {
       _id: { type: ObjectId, auto: true },
       name: { type: String, required: true },
       permissions: { type: Array, default: [], enum: PERMISSIONS },
       status: { type: String, enum: Object.values(ENUMS.STATUS), default: ENUMS.STATUS.ACTIVE }
   },
  merchant_group: {
    name: { type: String, required: true },
    merchants: [{ type: ObjectId }],
    locations: [{ type: ObjectId }]
  },
  status: { type: String, enum: Object.values(ENUMS.STATUS), default: ENUMS.STATUS.INACTIVE },
  devices: [{
      _id: false,
      os: { type: String, enum: _.values(ENUMS.DEVICES_OS) },
      uuid: { type: String }
   }],
  date_created: { type: Date, default: Date.now },
  date_modified: { type: Date, default: Date.now }
}

Furthermore:

PERMISSIONS = [
  'activity-list',
  'advert-add',
  'advert-view',
  'advert-edit'
  ...
];

and

ENUMS.DEVICES_IOS = {
  IOS: 'ios',
  ANDROID: 'android',
  WEB: 'web'
}

The issue is permissions: { type: Array, default: [], enum: PERMISSIONS }. enum is a no-op on arrays of non-strings, and before 5.0.6 setting enum on an array did nothing. You should do:

permissions: { type: [String], enum: PERMISSIONS }

The default: [] is also unnecessary, mongoose creates an empty array by default.

Not sure what that means but your suggestion fixed it. Thank you.

@alejandromagnorsky he meant that mongoose does nothing if you set enum for an array of non-strings. So if your type is Array and not [String] mongoose will just ignore it.

The issue is permissions: { type: Array, default: [], enum: PERMISSIONS }. enum is a no-op on arrays of non-strings, and before 5.0.6 setting enum on an array did nothing. You should do:

permissions: { type: [String], enum: PERMISSIONS }

The default: [] is also unnecessary, mongoose creates an empty array by default.

Just fixed my issue.

Was this page helpful?
0 / 5 - 0 ratings