【发布时间】:2020-08-19 00:56:15
【问题描述】:
当我使用下面的猫鼬模式添加时,验证将起作用,但是当我更新时,即使我将一些必填字段留空,验证仍然会通过。我错过了什么
const mongoose = require('mongoose')
const slug = require("mongoose-slug-generator")
mongoose.plugin(slug)
const imagePath = 'uploads/blogImages'
const blogSchema = new mongoose.Schema({
title: {
type: String,
required: [true, "Blog Title is required"],
trim: true,
},
image: {
type: String,
required: [true, "Blog Image is required"],
},
desc: {
type: String,
required: [true, "Blog Body is required"],
},
slug: {
type: String,
slug: "title"
},
}, {
timestamps: true,
});
blogSchema.virtual('imagePath').get(function() {
if (this.image != null) {
return path.join("/", imagePath, this.image);
}
})
const blogModel = mongoose.model('blogs', blogSchema)
module.exports = blogModel
module.exports.imagePath = imagePath
【问题讨论】: