typegoose 中枚举的建模列表

Modeling list of enums in typegoose

我一直在尝试像下面这样在 typegoose 中对枚举数组进行建模,但一直出现编译错误。

export enum USER_ROLES {
    ADMIN = 'admin',
    SUBSCRIBER = 'subs',
    NONE = 'none',
}


export class User {
    @prop({ type: () => [String], enum: USER_ROLES, default: [USER_ROLES.SUBSCRIBER] })
    roles?: USER_ROLES[];
}

export const UserModel = getModelForClass(User, {
    schemaOptions: {
        collection: 'users',
        timestamps: {
            createdAt: 'createdAt',
            updatedAt: 'createdAt',
        },
    }
});

我收到的错误信息是:

错误:“User.roles”的类型无效!类型是:“function String() { [native code] }” [E009]

请问我该如何正确操作?

今天早上我遇到了类似的问题,正确的做法如下:

export class User {
    @prop({ type: String, enum: USER_ROLES, default: [USER_ROLES.SUBSCRIBER] })
    roles?: USER_ROLES[];
}

type 告诉数组将保存的对象类型。在这种情况下,它将 String 其值可以是 adminsubsnone.

中的一个或多个