Mongoose preupdateOne挂接在更新文档时未被调用

jvlzgdj9  于 2022-11-13  发布在  Go
关注(0)|答案(2)|浏览(102)

我有这样的架构,像basicSalary这样的字段可以由管理员通过 Jmeter 板UI进行编辑,pre保存钩子可以很好地计算字段,然后pre updateOne钩子可以在管理员编辑文档时更新文档

const salariesSchema = mongoose.Schema({
  employeeId: {
    type: mongoose.Schema.Types.ObjectId,
    ref: "employee",
    required: true,
  },
  month: { type: String, required: true },
  year: { type: String, required: true },
  basicSalary: { type: Number, default: 0, required: true },
  accomodation: { type: Number, default: 0 },
  transportation: { type: Number, default: 0 },
  bonus: { type: Number, default: 0 },
  SSC: { type: Number, default: 0 },
  incomeTax: { type: Number, default: 0 },
  medicalInsurance: { type: Number, default: 0 },
  loan: { type: Number, default: 0, default: null },
  totalEarnings: { type: Number },
  totalDeductions: { type: Number },
  netSalary: { type: Number },
});
salariesSchema.pre("save", function (next) {
  this.SSC = this.basicSalary * 0.07;
  this.totalEarnings =
    this.basicSalary + this.accomodation + this.transportation + this.bonus;
  this.totalDeductions =
    this.incomeTax + this.medicalInsurance + this.loan + this.SSC;
  this.netSalary = this.totalEarnings - this.totalDeductions;
  next();
});

salariesSchema.pre("updateOne", function (next) {
  this.SSC = this.basicSalary * 0.07;
  this.totalEarnings =
    this.basicSalary + this.accomodation + this.transportation + this.bonus;
  this.totalDeductions =
    this.incomeTax + this.medicalInsurance + this.loan + this.SSC;
  this.netSalary = this.totalEarnings - this.totalDeductions;
  next();
});

路线〉薪金..js

const Salary = require("../models/Salary");

const editSalary = async (req, res) => {
  try {
    const salary = Salary.findById(req.body._id);
    await salary.updateOne({ $set: req.body });
    res.status(200).json("salary has been updated successfully");
  } catch (err) {
    console.log(err);
    res.status(400).json(err);
  }
};

例如,如果admin将basicSalary增加了50,则totalEarningsnetSalary的值也应该根据pre updateOne挂钩中的计算更新50,但这不起作用,这里出了什么问题?

2vuwiymt

2vuwiymt1#

您应该为其他类型的查询创建单独的pre挂钩。例如,您可以添加updateOnepre挂钩:

salariesSchema.pre("save", function (next) {
  this.SSC = this.basicSalary * 0.07;
  this.totalEarnings =
    this.basicSalary + this.accomodation + this.transportation + this.bonus;
  this.totalDeductions =
    this.incomeTax + this.medicalInsurance + this.loan + this.SSC;
  this.netSalary = this.totalEarnings - this.totalDeductions;
  next();
});

salariesSchema.pre('updateOne', function() {
  this.SSC = this.basicSalary * 0.07;
  this.totalEarnings =
    this.basicSalary + this.accomodation + this.transportation + this.bonus;
  this.totalDeductions =
    this.incomeTax + this.medicalInsurance + this.loan + this.SSC;
  this.netSalary = this.totalEarnings - this.totalDeductions;
  next();
});
rta7y2nd

rta7y2nd2#

我发现我不能直接使用this.fieldName访问字段,我通过使用转换为数字的this.get("fieldName"),并使用this.set(参见文档)而不是return fieldName = expression来解决它
“如果你有更好的方法,请告诉我"

salariesSchema.pre("updateOne", function (next) {
  this.set({ SSC: Number(this.get("basicSalary")) * 0.07 });
  this.set({
    totalDeductions:
      Number(this.get("incomeTax")) +
      Number(this.get("medicalInsurance")) +
      Number(this.get("loan")) +
      Number(this.get("SSC")),
  });

  this.set({
    totalEarnings:
      Number(this.get("basicSalary")) +
      Number(this.get("accomodation")) +
      Number(this.get("transportation")) +
      Number(this.get("bonus")),
  });

  this.set({
    netSalary:
      Number(this.get("totalEarnings")) - Number(this.get("totalDeductions")),
  });

  next();
});

相关问题