【发布时间】:2021-08-30 12:55:35
【问题描述】:
我有以下猫鼬模型。我为状态设置了一个枚举,但状态仍然接受除枚举之外的任何值。我在这里做错了什么?当我输入 ABCDEF 作为值时,它仍然保存在 mongodb 中。
import { model, Schema } from 'mongoose';
import Joi from '@hapi/joi';
import { USER_STATUSES } from 'enums';
const userSchema = new Schema({
firstName: {
type: String,
minlength: 1,
maxlength: 20,
required: true,
trim: true,
},
lastName: {
type: String,
minlength: 1,
maxlength: 20,
required: true,
trim: true,
},
email: {
type: String,
minlength: 5,
maxlength: 50,
required: true,
trim: true,
unique: true,
},
mobile: {
type: String,
minlength: 5,
maxlength: 50,
required: true,
trim: true,
},
password: {
type: String,
maxlength: 255,
required: true,
},
status: {
type: String,
enum: ['Pending', 'Active'],
default: 'ABCDEF,
},
date: {
type: Date,
default: Date.now,
},
});
export default model('User', userSchema);
【问题讨论】:
-
首先你应该像这样导出你的用户模型:module.exports = model('User', userSchema);其次你如何保存用户,你可以在那个问题中添加代码吗?
-
你为什么不把 'ABCDEF' 添加到你的枚举中: enum: ['Pending', 'Active', 'ABCDEF'], ?