每次创建文章并将其保存在数据库中时,如何生成一个 slug?
How to generate a slug everytime an article is created and saved in the database?
所以,我有这个 articleSchema,我想在其中创建一个独特的 slug。
const mongoose = require("mongoose")
const Schema = mongoose.Schema
var URLSlug = require('mongoose-slug-generator')
const articleSchema = new Schema({
title: { type: String, required: true },
description: { type: String, required: true },
userId: { type: Schema.Types.ObjectId, ref: "User" },
slug: { type: "String", slug: "title", unique: true }
}, { timestamps: true })
articleSchema.pre("save", function(next) {
this.slug = this.title.split(" ").join("-")
next()
})
articleSchema.plugin(URLSlug("title", {field: "Slug"}))
const Article = mongoose.model("Article", articleSchema)
module.exports = Article
这里是 articleController
newArticle: (req, res) => {
Article.create(req.body, (err, newArticle) => {
if (err) {
return res.status(404).json({ error: "No article found" })
} else {
return res.status(200).json({ article: newArticle })
}
})
}
我不知道,当我在 postman 中查看时,它说找不到文章,更不用说 slug 了!另外,我收到此错误:
schema.eachPath is not a function
根据mongoose-slug-generator,您需要在 mongoose 上应用插件,但在您的代码中,它已应用于模式。
因此,如果您尝试使用此代码,它将起作用:
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
var URLSlug = require("mongoose-slug-generator");
mongoose.plugin(URLSlug);
const articleSchema = new Schema(
{
title: { type: String, required: true },
description: { type: String, required: true },
userId: { type: Schema.Types.ObjectId, ref: "User" },
slug: { type: String, slug: "title"}
},
{ timestamps: true }
);
articleSchema.pre("save", function(next) {
this.slug = this.title.split(" ").join("-");
next();
});
const Article = mongoose.model("Article", articleSchema);
module.exports = Article;
如果我们像这样发送 req.body:
{
"title": "metal head dev",
"userId": "5e20954dc6e29d1b182761c9",
"description": "description"
}
保存的文档将是这样的(如您所见,slug 已正确生成):
{
"_id": "5e23378672f10f0dc01cae39",
"title": "metal head dev",
"description": "description",
"createdAt": "2020-01-18T16:51:18.445Z",
"updatedAt": "2020-01-18T16:51:18.445Z",
"slug": "metal-head-dev",
"__v": 0
}
顺便说一句 mongoose-slug-generator
看起来很老了,有一个更受欢迎且维护良好的 slugify 包。
所以,我有这个 articleSchema,我想在其中创建一个独特的 slug。
const mongoose = require("mongoose")
const Schema = mongoose.Schema
var URLSlug = require('mongoose-slug-generator')
const articleSchema = new Schema({
title: { type: String, required: true },
description: { type: String, required: true },
userId: { type: Schema.Types.ObjectId, ref: "User" },
slug: { type: "String", slug: "title", unique: true }
}, { timestamps: true })
articleSchema.pre("save", function(next) {
this.slug = this.title.split(" ").join("-")
next()
})
articleSchema.plugin(URLSlug("title", {field: "Slug"}))
const Article = mongoose.model("Article", articleSchema)
module.exports = Article
这里是 articleController
newArticle: (req, res) => {
Article.create(req.body, (err, newArticle) => {
if (err) {
return res.status(404).json({ error: "No article found" })
} else {
return res.status(200).json({ article: newArticle })
}
})
}
我不知道,当我在 postman 中查看时,它说找不到文章,更不用说 slug 了!另外,我收到此错误:
schema.eachPath is not a function
根据mongoose-slug-generator,您需要在 mongoose 上应用插件,但在您的代码中,它已应用于模式。
因此,如果您尝试使用此代码,它将起作用:
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
var URLSlug = require("mongoose-slug-generator");
mongoose.plugin(URLSlug);
const articleSchema = new Schema(
{
title: { type: String, required: true },
description: { type: String, required: true },
userId: { type: Schema.Types.ObjectId, ref: "User" },
slug: { type: String, slug: "title"}
},
{ timestamps: true }
);
articleSchema.pre("save", function(next) {
this.slug = this.title.split(" ").join("-");
next();
});
const Article = mongoose.model("Article", articleSchema);
module.exports = Article;
如果我们像这样发送 req.body:
{
"title": "metal head dev",
"userId": "5e20954dc6e29d1b182761c9",
"description": "description"
}
保存的文档将是这样的(如您所见,slug 已正确生成):
{
"_id": "5e23378672f10f0dc01cae39",
"title": "metal head dev",
"description": "description",
"createdAt": "2020-01-18T16:51:18.445Z",
"updatedAt": "2020-01-18T16:51:18.445Z",
"slug": "metal-head-dev",
"__v": 0
}
顺便说一句 mongoose-slug-generator
看起来很老了,有一个更受欢迎且维护良好的 slugify 包。