Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

programmatically change mongoose schema enum values

//My field is called "status" inside my schema. So "validate" will check if can pass or not.
status: { type: String, default: 'Abierta', validate: (v) => {
    return customEnum(v, 'Status');
}},

// And this is my "customEnum" module
// This is the collection where I store the data. I don't want to use only, status, so
// I made it dynamic, to choose what I want to retrieve.
const SingleValue = require('../models/SingleValue');

module.exports = async (v, type) => {
    return !!await SingleValue.findOne({ typeOf: type, deleted: false, value: v });
}
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #programmatically #change #mongoose #schema #enum #values
ADD COMMENT
Topic
Name
9+4 =