猫鼬模式“ this”关键字返回未定义

时间:2019-11-10 16:45:36

标签: mongoose

我有以下具有两个字段的架构:

-finalGrade由用户输入

-numericGrade源自finalGrade

问题是:无论我从finalGrade枚举中输入哪个值,numericalGrade始终为164。当我尝试转义this.finalGrade的值时,我发现它始终返回“未定义”。

如何获取正确的值?

谢谢!

(参考:https://mongoosejs.com/docs/defaults.html#default-functions-and-this

编辑2:整个架构已更新

const mongoose = require('mongoose');

const Student = mongoose.model('Student');
const Course = mongoose.model('Course');

let enrolmentSchema = new mongoose.Schema({

        student: {
            type: Number, 
            min: 1000000000, 
            max: 9999999999, 
            required: [true, "missing studentID"],
            validate:{
                isAsync: true,
                validator: function(v, cb){
                    Student.find({studentID: v}, function(err,docs){
                        cb(docs.length != 0);
                    })
                },
                message: 'Student does not exist...'
            }

        },
        course: {type: String, minlength: 3, maxlength: 3, required: [true, "missing courseID"]},
        semester:{
            type: Number, 
            validate:
            {
                validator: function(v){
                    return Number.isInteger(v) && v>= 1 && v<= 8;
                },
            message: "semester must be an integral value from [1 to 8]"
            }
        },
        finalGrade: {
            type: String,
            enum: ['E', 'G', 'P', 'F', '']
        },

        numericGrade:{
            type: Number,
            default: function()
            {
                console.log(escape("This is finalGrade:" +this.finalGrade)); //debugging
                if(this.finalGrade==='E'){
                    return 5;
                }
                else if(this.finalGrade==='G'){
                    return 4;
                }
                else if(this.finalGrade==='P'){
                    return 3;
                }
                else if(this.finalGrade==='F'){
                    return 2;
                }
                else if(this.finalGrade===''){
                    return 1;
                }
                else return 164;
            }
        }

})
mongoose.model('Enrolment', enrolmentSchema);

编辑:根据要求添加了文档创建代码:

function insertEnrolmentRecord(req,res){
    var enrolment = new Enrolment();
    enrolment.student = req.body.student;
    enrolment.course = req.body.course;
    enrolment.semester= req.body.semester;
    enrolment.finalGrade= req.body.finalGrade;
    enrolment.save((err,doc)=>{
        if(err) {
            console.log(err);
            res.redirect('/enrolment/add_failed')
        }
        else res.redirect('/enrolment');

   })
}

1 个答案:

答案 0 :(得分:0)

由于某种原因,当您像这样创建注册文档时,默认选项似乎不起作用。

但是当您使用构造函数表单时,它似乎可以工作。

尝试这样:

function insertEnrolmentRecord(req, res) {

  const { student, course, semester, finalGrade } = req.body;

  let enrolment = new Enrolment({ student, course, semester, finalGrade });

  console.log(enrolment.numericGrade); // even here it will work

  enrolment.save((err, doc) => {
    if (err) {
      console.log(err);
      res.redirect('/enrolment/add_failed')
    }
    else res.redirect('/enrolment');

  })
}