Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

149
Views
Calculation when sending data through Mongoose to MongoDb

Im trying to calculate a users BMI and send it through to my MongoDB atlas db, the bmi will be calculated through the height and weight that users enter, I've set up a Mongoose Schema that set out the function but at this point my bmi is not showing on the post request, can anyone steer me in the right direction?

const mongoose = require('mongoose');

const users = mongoose.Schema({
    username:{
        type: String,
        required: true
    },
    first:{
        type: String,
        required: true
    },
    last:{
        type: String,
        required: true
    },
    occupation:String,
    profile:{
        age:{
            type: Number,
            required: true
        },
        sex:{
            type: String,
            required: true
        },
        height:{
            type: Number,
            required: true
        },
        weight:{
            type: Number,
            required: true
        },
//Issue is here. Trying to use the Schema to 'pre' calculate
        bmi:{
            type: Number,
            get: () =>{
                let BMI = weight/height ** 2
                return BMI
            },
            set: BMI => BMI,
            alias: 'bmi',

        },
        eyeColor: String,
        incomePM: Number,
        interestedIn: String
    }
})

module.exports = mongoose.model('users', users);
about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Try using the pre middleware of mongoose,

const mongoose = require('mongoose');

const users = mongoose.Schema({
    username:{
        type: String,
        required: true
    },
    first:{
        type: String,
        required: true
    },
    last:{
        type: String,
        required: true
    },
    occupation:String,
    profile:{
        age:{
            type: Number,
            required: true
        },
        sex:{
            type: String,
            required: true
        },
        height:{
            type: Number,
            required: true
        },
        weight:{
            type: Number,
            required: true
        },
        bmi:{
            type: Number,
        },
        eyeColor: String,
        incomePM: Number,
        interestedIn: String
    }
})

users
 .pre('save', function(next){
  console.log(this.profile.weight);
  console.log(this.profile.height);
  this.profile.bmi = 
    (this.profile.weight/this.profile.height) ** 2;
  next();   
 });
module.exports = mongoose.model('users', users);
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!