javascript - Mongoose 枚举数

标签 javascript node.js mongodb mongoose enums

我需要获取架构中字段的枚举值

我有架构:

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'];

如何获取数字类型的枚举数组?

最佳答案

要指定数值范围,您可以在架构中定义 minmax 值:

role: {
    type: Number,
    min: 0,
    max: 2,
    default: 1
},

文档 here .

还要求值是整数,请参阅 here .

关于javascript - Mongoose 枚举数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42303758/

相关文章:

javascript - 奇怪的(webpack?)错误 "TypeError: Object(...) is not a function"

javascript - 如何使用 Angular Fullstack 部署 Throng 或 Cluster?

java - 如何在 Spring Data Mongo 中的聚合投影字段中嵌套字段

javascript - 仅在暂存 JS 文件时运行 NPM 脚本

javascript - 让div水平环绕

javascript - 如何在按钮单击上添加类?

javascript - 如何使用 ffi 将 WinApi 函数加载到 Node.js 中?

javascript - 中止 rethinkdb 更改源

mongodb - 如何在Kubernetes环境上的mongoDB上远程初始化副本集

c# - 在 C# 中运行 Web 服务时 mongodb 数据丢失