[英]Error: File validation failed: content: Path `content` is required., title: Path `title` is required
I'm trying trying to add text data in the database and display it on the dashboard.我正在尝试在数据库中添加文本数据并将其显示在仪表板上。 I have created a separate route for adding files.我为添加文件创建了一条单独的路线。 I'm following Brad's video: NodejsfromScratch I'm currently stuck at Add files part (1:31:24)我正在关注 Brad 的视频: NodejsfromScratch我目前停留在添加文件部分 (1:31:24)
When I enter the text data in the add files form, I get this error:当我在添加文件表单中输入文本数据时,出现此错误:
Error: File validation failed: content: Path `content` is required., title: Path `title` is required.
at ValidationError.inspect (E:\EXP_NP\BT\node_modules\mongoose\lib\error\validation.js:47:26)
at internal/per_context/primordials.js:23:32
at formatValue (internal/util/inspect.js:783:19)
at inspect (internal/util/inspect.js:337:10)
at formatWithOptionsInternal (internal/util/inspect.js:2016:40)
at formatWithOptions (internal/util/inspect.js:1898:10)
at console.value (internal/console/constructor.js:330:14)
at console.warn (internal/console/constructor.js:363:61)
at E:\EXP_NP\BT\routes\files.js:24:16
at processTicksAndRejections (internal/process/task_queues.js:93:5) {
errors: {
content: ValidatorError: Path `content` is required.
at validate (E:\EXP_NP\BT\node_modules\mongoose\lib\schematype.js:1253:13)
at E:\EXP_NP\BT\node_modules\mongoose\lib\schematype.js:1236:7
at Array.forEach (<anonymous>)
at SchemaString.SchemaType.doValidate (E:\EXP_NP\BT\node_modules\mongoose\lib\schematype.js:1181:14)
at E:\EXP_NP\BT\node_modules\mongoose\lib\document.js:2517:18
at processTicksAndRejections (internal/process/task_queues.js:75:11) {
properties: [Object],
kind: 'required',
path: 'content',
value: undefined,
reason: undefined,
[Symbol(mongoose:validatorError)]: true
},
title: ValidatorError: Path `title` is required.
at validate (E:\EXP_NP\BT\node_modules\mongoose\lib\schematype.js:1253:13)
at E:\EXP_NP\BT\node_modules\mongoose\lib\schematype.js:1236:7
at Array.forEach (<anonymous>)
at SchemaString.SchemaType.doValidate (E:\EXP_NP\BT\node_modules\mongoose\lib\schematype.js:1181:14)
at E:\EXP_NP\BT\node_modules\mongoose\lib\document.js:2517:18
at processTicksAndRejections (internal/process/task_queues.js:75:11) {
properties: [Object],
kind: 'required',
path: 'title',
value: undefined,
reason: undefined,
[Symbol(mongoose:validatorError)]: true
}
},
_message: 'File validation failed'
}
My Files schema: (models/files)我的文件架构:(模型/文件)
const mongoose = require('mongoose');
const fileSchema = new mongoose.Schema({
title: {
type: String,
required:true,
trim: true
},
content: {
type: String,
required:true
},
user: {
type: mongoose.Schema.Types.ObjectId,
ref: "User"
},
createdAt: {
type: Date,
default: Date.now
}
})
module.exports = mongoose.model('File', fileSchema);
My Files route: (routes/files)我的文件路线:(路线/文件)
const express = require('express')
const router = express.Router()
const { ensureAuth } = require('../middleware/auth')
const File = require('../models/Files')
// @desc Show add page
// @route GET /files/add
router.get('/add', ensureAuth, (req, res) => {
res.render('files/add', {
layout: 'add',
title: 'Add',
})
})
// @desc Process add form
// @route POST /files
router.post('/', ensureAuth, async (req, res) => {
try {
req.body.user = req.user.id
await File.create(req.body)
res.redirect('/dashboard')
} catch (err) {
console.error(err);
res.render('error/500')
}
})
module.exports = router
My app.js我的 app.js
const express = require('express')
const dotenv = require('dotenv')
const morgan = require('morgan')
const path = require("path")
const exphbs = require("express-handlebars")
const passport = require('passport')
const session = require('express-session');
const MongoStore = require('connect-mongo');
// DATABASE CONNECTION
const connectDB = require('./config/db')
const { connection } = require('mongoose')
// Load config
dotenv.config({ path: './config/config.env' })
//Passport config
require('./config/passport')(passport)
connectDB()
const app = express()
// Body Parser
app.use(express.urlencoded({extended: false}))
app.use(express.json())
// Handlebars
app.engine('.hbs', exphbs({
defaultLayout: 'main',
extname: '.hbs',
partialsDir: __dirname + '/views/partials/',
layoutsDir: __dirname + '/views/layouts'
}))
app.set("view engine", "hbs");
app.use(express.static(__dirname + '/public'));
// Express Session Middleware
app.use(session({
secret: process.env.SECRET_KEY,
resave: false,
saveUninitialized: false,
store: MongoStore.create({ mongoUrl: process.env.MONGO_URI, collection: 'sessions' }),
cookie: {
maxAge: 1000 * 60 * 60 * 24 // 1 Day
}
}))
// Passport Middleware
app.use(passport.initialize());
app.use(passport.session());
// Routes
app.use('/',require('./routes/index'))
app.use('/users',require('./routes/users'))
app.use('/files',require('./routes/files'))
app.set("view engine", "hbs");
// app.set("views", template_path);
if(process.env.NODE_ENV === 'development') {
app.use(morgan('dev'))
}
const PORT = process.env.PORT || 3000
app.listen(PORT, () => {
console.log(`Server running in ${process.env.NODE_ENV} mode on PORT ${PORT}`);
});
My html file: *(views/files/add)我的 html 文件:*(查看/文件/添加)
<form action="/files" method="POST">
<div class="form">
<p>
<label>Title <span>*</span> </label>
<input type="text" class="field size" />
</p>
<p>
<label>Content <span>*</span> </label>
<textarea name="body" id="body" class="field size" rows="10" cols="30"></textarea>
</p>
<input type="submit" value="Save" class="btn btn-outline-dark btn-lg">
</div>
</form>
My layout file: *(views/layouts/add)我的布局文件:*(视图/布局/添加)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Add Files</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.1/css/all.min.css"
integrity="sha512-+4zCK9k+qNFUR5X+cKL9EIR+ZOhtIloNl9GIKS57V1MyNsYpYcUrUeQc9vNfzsWfV28IaLL3i96P9sdNyeRssA=="
crossorigin="anonymous" />
<link rel="stylesheet" href="/css/add.css" />
<title>{{ title }}</title>
</head>
<body>
{{{body}}}
<script src="https://cdnjs.cloudflare.com/ajax/libs/ckeditor/4.16.0/ckeditor.js" integrity="sha512-7My1gsUz5JUQgT8+P0sHKaPel/77X3zjGZsXbTS8Y7MhDEJ+f9xg9H+pPzONFL5djye0zWLlxFLApGsWQ1gdfA==" crossorigin="anonymous"></script>
<script>
CKEDITOR.replace('body', {
plugins: 'wysiwygarea, toolbar, basicstyles, link'
})
</script>
</body>
</html>
My Input:我的输入:
Title : Lorem Ipsum Content : Lorem Ipsum is simply dummy text of the printing and typesetting industry.标题: Lorem Ipsum内容: Lorem Ipsum 只是印刷和排版行业的虚拟文本。 Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.自 1500 年代以来,Lorem Ipsum 一直是行业的标准虚拟文本,当时一位不知名的印刷商采用了一种类型的厨房并将其加扰以制作一本类型样本书。 It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.它不仅经历了五个世纪,而且经历了电子排版的飞跃,基本保持不变。 It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.它在 1960 年代随着包含 Lorem Ipsum 段落的 Letraset 表的发布而流行起来,最近还随着 Aldus PageMaker 等桌面出版软件(包括 Lorem Ipsum 的版本)而普及。
Your title input doesn't have a name and your textarea is named body.您的标题输入没有名称,您的 textarea 被命名为 body。 Add name="title"
to input and change textarea name to content
添加name="title"
以输入并将 textarea 名称更改为content
<form action="/files" method="POST">
<div class="form">
<p>
<label>Title <span>*</span> </label>
<input name="title" type="text" class="field size" />
</p>
<p>
<label>Content <span>*</span> </label>
<textarea name="content" id="body" class="field size" rows="10" cols="30"></textarea>
</p>
<input type="submit" value="Save" class="btn btn-outline-dark btn-lg">
</div>
</form>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.