Slugify a Field in Mongoose Schema

PHOTO EMBED

Saved by @mishka #javascript #nodejs

var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var slug = require('mongoose-slug-generator');

mongoose.plugin(slug);

const pageSchema = new Schema({
    title: { type: String , required: true},
    slug: { type: String, slug: "title" }
});

var Page = mongoose.model('Page', pageSchema);
module.exports = Page;
content_copyCOPY

First install express package: mongoose-slug-generator npm install mongoose-url-slugs