带有静态吸气剂的Mongoose ES6类

时间:2019-03-15 12:45:07

标签: javascript mongoose ecmascript-6

有什么方法可以访问装载了猫鼬的ES6类的静态吸气剂

在下面的代码中,我想通过猫鼬模型访问静态获取器“ STATUS”。

const STATUS = {
    PRE_ACTIVE: 'pre-active',
    LIVE: 'live',
};

class Student {
    static get STATUS() {
        return STATUS;
    }

    static attributes() {
        return {
            name: {type: String, required: true},
            lastName: {type: String, required: true},
            language: {type: String, required: true},
            gender: {type: String, required: true},
        }
    }

    get fullName() {
        return `${this.name}_${this.lastName}`;
    }
}
const schema = new Schema(Student.attributes(), {timestamps: true});
schema.loadClass(Student);

const StudentMongoose = mongoose.model('Student', schema);
StudentMongoose.find({}); // -> OK

const instance = new StudentMongoose({name: 'John', lastName: 'Doe', language: 'en', gender: 'male'});
instance.fullName; // -> OK

StudentMongoose.attributes(); // -> OK

StudentMongoose.STATUS; // -> NOK

0 个答案:

没有答案