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

174
Vistas
Mongoose: pre('validate') middleware is not working

This is my mongoose Setup. This happens only when i use class syntax. When i do the same thing with the use of functional programming it works fine. This is the first time i am using class syntax to do this. I think that's where the problem lies. I am doing something wrong with my class definition. This is my mongoose Setup. This happens only when i use class syntax. When i do the same thing with the use of functional programming it works fine. This is the first time i am using class syntax to do this. I think that's where the problem lies. I am doing something wrong with my class definition.

const mongooseService = require('./services/mongoose.service')
const slugify = require('slugify')
const { marked } = require('marked')
const createDomPurifier = require('dompurify')
const { JSDOM } = require('jsdom')
const dompurify = createDomPurifier(new JSDOM().window)

class ArticleDao {
  Schema = mongooseService.getMongoose().Schema

  articleSchema = new this.Schema({
    title: {
      type: String,
      required: true,
    },
    description: {
      type: String,
    },
    markdown: {
      type: String,
      required: true,
    },
    createdAt: {
      type: Date,
      default: new Date(),
    },
    slug: {
      type: String,
      required: true,
      unique: String,
    },
    sanitizedHtml: {
      type: String,
      required: true,
    },
  })

  Article = mongooseService.getMongoose().model('Article', this.articleSchema)

  constructor() {
    console.log(`created new instance of DAO`)
    this.setPreValidation()
  }

  setPreValidation() {
    console.log('h')
    this.articleSchema.pre('save', (next) => {
      if (this.title) {
        this.slug = slugify(this.title, { lower: true, strict: true })
      }
      if (this.markdown) {
        this.sanitizedHtml = dompurify.sanitize(marked(this.markdown))
      }
      next()
    })
  }

  async addArticle(articleFields) {
    const article = new this.Article(articleFields)
    await article.save()
    return article
  }

  async getArticleById(articleId) {
    return this.Article.findOne({ _id: articleId }).exec()
  }

  async getArticleBySlug(articleSlug) {
    return this.Article.findOne({ slug: articleSlug })
  }

  async getArticles() {
    return this.Article.find().exec
  }

  async updateArticleById(articleId, articleFields) {
    const existingArticle = await this.Article.findOneAndUpdate({
      _id: articleId,
      $set: articleFields,
      new: true,
    }).exec()
    return existingArticle
  }

  async removeArticleById(articleId) {
    await this.Article.findOneAndDelete({ _id: articleId }).exec()
  }
}

module.exports = new ArticleDao()

This is the error i get:

Article validation failed: sanitizedHtml: Path `sanitizedHtml` is required., slug: Path `slug` is required.
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