Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

150
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda