Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

171
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda