我需要获取模式中字段的枚举值
我有架构:
let adminSchema = new Schema({
login: {
type: String,
unique: true,
required: true,
minlength: 5,
maxlength: 300
},
hashedPassword: {
type: String
},
role: {
type: Number,
enum: [0, 1, 2],
default: 1
},
salt: {
type: String
}
});
module.exports.Admin = Admin;
module.exports.roleEnum = Admin.schema.path('role').enumValues;
console.log(module.exports.roleEnum);
控制台日志 -> 未定义
但如果我将角色字段类型更改为字符串
let adminSchema = new Schema({
login: {
type: String,
unique: true,
required: true,
minlength: 5,
maxlength: 300
},
hashedPassword: {
type: String
},
role: {
type: String,
enum: ['0', '1', '2'],
default: '1'
},
salt: {
type: String
}
});
module.exports.Admin = Admin;
module.exports.roleEnum = Admin.schema.path('role').enumValues;
console.log(module.exports.roleEnum);
控制台日志 -> ['0', '1', '2'];
我如何获取数字类型的枚举数组?
要指定数值范围,您可以定义min
and max
架构中的值:
role: {
type: Number,
min: 0,
max: 2,
default: 1
},
Docs here http://mongoosejs.com/docs/api.html#schema-number-js.
如果还要求这些值是整数,请参阅here https://stackoverflow.com/questions/37711302/validate-integer-values-with-mongoose.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)