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

151
Views
The mongoose .pre() hook do not trigger as it should

I'm trying to change a field af a document as another one gets updated, but fot some reason that i'm not seeing, it´s no happening.

The field "marked" gets the value of the field "content" as the document gets criated, as it should, but when the ducument get updated, the value of the field "marked" don't get updated.

(I'm using marked and sanitize-html to render markdown)

const mongoose = require('mongoose')
const marked = require('marked')
const sanitizeHtml = require('sanitize-html')

const postSchema = new mongoose.Schema({ 
    title: {
        type: String,
        required: true
    },
    desc: {
        type: String        
    },
    content: {
        type: String,
        required: true
    },
    marked: {
        type: String,
        required: true
    }
});

postSchema.pre('validate', function(next) { // alredy tried save, and updateOne
    if (this.content) {
        this.marked = sanitizeHtml(marked.parse(this.content)) 
    }

    next() 
})


module.exports = mongoose.model('Post', postSchema)

about 4 years ago · Juan Pablo Isaza
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!