我需要获取架构中字段的枚举值
我有架构:
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);
控制台日志 -> 未定义
但是如果我将 Angular 色字段类型更改为字符串
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
和 max
值:
role: {
type: Number,
min: 0,
max: 2,
default: 1
},
文档 here .
还要求值是整数,请参阅 here .
关于javascript - Mongoose 枚举数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42303758/