Home > Software design >  can't use fixed value in default function in Mongoose schema?
can't use fixed value in default function in Mongoose schema?

Time:09-22

im trying to calculate SSC which is calculated by multiplying basicSalary by 0.07%, in totalEarnings, totalDeductions and netSalary it works fine by doing calculations on fields, but in SSC multiplying basicSalary by 0.07% gives an error expression expected, does that i mean i only can use fields ?

const mongoose = require("mongoose");

const salariesSchema = mongoose.Schema({

  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: function(){
    return this.basicSalary * 0.07%
  } },

  incomeTax: { type: Number, default: 0 },
  medicalInsurance: { type: Number, default: 0 },
  loan: { type: Number, default: 0, default: null },

  totalEarnings: {
    type: Number,
    default: function () {
      return (
        this.basicSalary   this.accomodation   this.transportation   this.bonus
      );
    },
  },

  totalDeductions: {
    type: Number,
    defualt: function () {
      return this.SSC - this.incomeTax - this.medicalInsurance - this.loan;
    },
  },

  netSalary: {
    type: Number,
    default: function () {
      return this.totalEarnings - this.totalDeductions;
    },
  },
});

module.exports = mongoose.model("salarie", salariesSchema);

CodePudding user response:

If you want to calculate the 7% of the value you should write:

SSC: { 
    type: Number, 
    default: function () {
        return this.basicSalary * 0.07;
    } 
}
  • Related