我很乐意在我的整体成绩上添加prelimGrade,midtermGrade,finalsGrade,并将其除以3。此代码为我提供了空值。
我尝试在此处搜索并找到解决方案的问题,但该值也给了我空值。
这是我在Test.js模式上的代码
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
const TestSchema = new Schema({
prelim: { type: Number, default: 1 },
midterm: { type: Number, default: 1 },
finals: { type: Number, default: 1 },
overall: { type: Number }
});
module.exports = Test = mongoose.model("tests", TestSchema);
TestSchema.pre("save", function(next) {
this.overall = (this.prelim + this.midterm + this.finals)/3;
next();
});
这是我在路线上的代码
router.post("/test", (req, res) => {
const { prelim, midterm, finals, overall } = req.body;
const test = new Test({
prelim,
midterm,
finals,
overall
});
test.save().then(test => {
res.json(test);
});
});
我希望它能给我带来价值,但是却给我空值。
答案 0 :(得分:0)
module.exports应该在testSchema之后。这对我有用