I want to generate an slug when update is called in mongoose, now I have this and it works.
schema.pre('update', function (next) {
const title = this._update.$set['title']
this._update.$set['slug'] = slugify(title.toLowerCase())
next()
})
The problem is that slug is a unique: true field so if add two posts with the same title I get an error, for example like this:
"slug: post-2\" already exists."
So now before calling next() I want to be sure if my slug already exists calling find, now I have this code:
.pre('update', function (next) {
const title = this._update.$set['title']
this._update.$set['slug'] = slugify(title.toLowerCase())
this._find({ slug }, function (err, docs) {
if (err)
console.log(err)
else
console.log(docs)
next()
})
})
But when is called I get the following error:
? callback(null, docs)
^
TypeError: callback is not a function
How should I continue?
via user3561335
No comments:
Post a Comment