我在使用 Node、Mongo、Express 和 Edge 进行视图的简单节点应用程序时遇到了困难。
当我尝试通过视图中的表单提供数据时,我收到以下消息。
“无法发布/破折号/日期/创建”
我有一种感觉,这与我无法确定的粗心错误有关,因为我在以前的项目中已经这样做了,没有任何问题。我回顾了那些项目,仍然找不到我哪里出错了。这里的人可能很快就能找到它。
date_create.edge(查看)
<!--title-->
<div class="form-group">
<label for="title">Title</label>
<input type="text" placeholder="Title" id="title" name="title" required="true" class="form-control" value=""/>
</div>
<br>
<!--class-->
<div class="form-group">
<label for="class">Class</label>
<input type="text" placeholder="Class" id="class" name="class" required="true" class="form-control" value=""/>
</div>
<br>
<!--description-->
<div class="form-group">
<label for="description">Description</label>
<textarea type="text" placeholder="Description" id="description" name="description" required="true" class="form-control" value=""></textarea>
</div>
<br>
<!--date-->
<div class="form-group">
<label for="date">Date</label>
<input type="number" placeholder="Date" id="date" name="date" required="true" class="form-control" value=""/>
</div>
<br>
<!--day-->
<div class="form-group">
<label for="day">Day</label>
<input type="text" placeholder="Day" id="day" name="day" required="true" class="form-control" value=""/>
</div>
<br>
<!--month-->
<div class="form-group">
<label for="month">Month</label>
<input type="text" placeholder="Month" id="month" name="month" required="true" class="form-control" value=""/>
</div>
<br>
<!--time-->
<div class="form-group">
<label for="time">Time</label>
<input type="text" placeholder="Time" id="time" name="time" required="true" class="form-control" value=""/>
</div>
<br>
<br>
<button type="submit">Submit</button>
Date.js(模型)
const mongoose = require('mongoose');
const DateSchema = new mongoose.Schema({
title: String,
class: String,
description: String,
date: Number,
day: String,
month: String,
time: String
});
const Date = mongoose.model('Date', DateSchema);
module.exports = Date
路由.js
//--REQUIREMENTS--\\
const express = require('express');
const router = express.Router();
//--CONTROLLERS--\\
const article_controller = require('../Controllers/article_controller');
const class_controller = require('../Controllers/class_controller');
const dash_controller = require('../Controllers/dash_controller');
const date_controller = require('../Controllers/date_controller');
//--DASHBOARD--\\
router.get('/', dash_controller.index);
//--DATE--\\
// GET request for creating a Date
router.get('/date/create', date_controller.date_create_get);
// POST request for creating a Date
router.post('date/create', date_controller.date_create_post);
date_controller.js(控制器)
const { body,validationResult } = require('express-validator/check');
const { sanitizeBody } = require('express-validator/filter');
const Date = require('../Models/Date');
const async = require('async');
//--DATE--\\
// GET request for creating a Date
exports.date_create_get = function(req, res){
res.render('date_create')
}
// POST request for creating a Date
exports.date_create_post = [
//Validate Fields
body('title').isLength({ min: 1 }).trim().withMessage('Title must be specified.'),
body('class').isLength({ min: 1 }).trim().withMessage('Class must be specified.'),
body('description').isLength({ min: 1 }).trim().withMessage('Description must be specified.'),
body('date').isLength({ min: 1 }).trim().withMessage('Date must be specified.'),
body('day').isLength({ min: 1 }).trim().withMessage('Day must be specified.'),
body('month').isLength({ min: 1 }).trim().withMessage('Month must be specified.'),
body('time').isLength({ min: 1 }).trim().withMessage('Time must be specified.'),
//Sanitize Fields
sanitizeBody('title').trim().escape(),
sanitizeBody('class').trim().escape(),
sanitizeBody('description').trim().escape(),
sanitizeBody('date').trim().escape(),
sanitizeBody('day').trim().escape(),
sanitizeBody('month').trim().escape(),
sanitizeBody('time').trim().escape(),
//Process request after validation and sanitation.
(req, res, next) => {
//Extract the validation errors from a request.
const errors = validationResult(req);
if (!errors.isEmpty()) {
// There are errors. Render form again with sanitized values/errors messages.
res.render('date_create', { title: 'Create Date', date: req.body, errors: errors.array() });
return;
}
else {
//Data form form is valid
// Create Date object with escaped and trimmed data.
// title, class, description, date, day, month, time
var date = new Date(
{
title: req.body.title,
class: req.body.class,
description: req.body.description,
date: req.body.date,
day: req.body.day,
month: req.body.month,
time: req.body.time
});
date.save(function (err) {
if (err) {return next(err);}
//Successful - redirect
res.redirect('date_list');
});
}
}
];
就像我说的,这里的某个地方可能只是一个粗心的错误。我几乎在我能想到的所有地方都使用了 console.log,并且应用程序在控制文件中没有响应。这让我相信表单(视图)是问题所在。