我正在尝试在Mongoose中创建和使用enum
类型。我检查过了,但是我没有得到正确的结果。我在程序中使用enum
,如下所示:
我的方案是:
var RequirementSchema = new mongooseSchema({
status: {
type: String,
enum : ['NEW','STATUS'],
default: 'NEW'
},
})
但我在这里有点困惑,我如何才能像在Java NEW("new")
中一样将enum
的值。如何根据可枚举值将enum
保存到数据库中。我在express node.js中使用它。
谢谢
发布于 2015-07-02 19:45:25
这里的枚举基本上是String对象。改为将枚举行更改为enum: ['NEW', 'STATUS']
。你有一个带有引号的打字错误。
发布于 2020-12-06 00:25:17
假设我们有一个枚举Role
,其定义如下
export enum Role {
ADMIN = 'ADMIN',
USER = 'USER'
}
我们可以像这样使用它作为类型:
{
type: String,
enum: Role,
default: Role.USER,
}
发布于 2021-01-08 18:21:27
如果你想使用TypeScript enum
,你可以在接口IUserSchema
中使用它,但是在模式中你必须使用array
(Object.values(userRole)
)。
export enum userRole {
admin = 'admin',
user = 'user'
}
const UserSchema: Schema = new Schema({
userType: {
type: String,
enum: Object.values(userRole),
default: userRole.user, required: true
},
});
export interface IUserSchema extends Document {
userType: userRole
}
https://stackoverflow.com/questions/29299477
复制相似问题