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)