我正在寻找通过快速路由HTML表单在数据库中添加代码的方法。该表单正在运行,并且已从用户输入正确将已清理的跟踪和办公室编号输出到控制台,但是对数据库的调用未完成。
有人知道为什么从快速路由启动POST时未调用DB吗?
npm start
> node-forms@1.0.0 start C:\node-forms
> nodemon src/server.js
[nodemon] 1.14.12
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: *.*
[nodemon] starting `node src/server.js`
body-parser deprecated undefined extended: provide extended option src\server.js:20:14
App running at http://localhost:3000
Sanitized: { trackngNumber: '6e56e77747474747', officeNumber: '5656' }
server.js
const express = require('express')
const router = express.Router()
const { check, validationResult } = require('express-validator/check')
const { matchedData } = require('express-validator/filter')
var mysql = require('mysql');
var con = mysql.createConnection({
host: "localhost",
user: "reguser",
password: '',
port: 3306
});
router.get('/', (req, res) => {
res.render('index')
})
router.get('/addtrackingnumber', (req, res) => {
res.render('addtrackingnumber', {
data: {},
errors: {}
})
})
router.post('/addtrackingnumber', [
check('trackngNumber')
.isLength({ min: 13 })
.withMessage('trackng Number is required')
.trim(),
check('officeNumber')
.isLength({ min:4, max:4 })
.withMessage('That office Number doesn‘t look right')
.trim()
], (req, res) => {
const errors = validationResult(req)
if (!errors.isEmpty()) {
return res.render('addtrackingnumber', {
data: req.body,
errors: errors.mapped()
})
}
const data = matchedData(req)
console.log('Sanitized:', data)
req.flash('success', 'Thanks this has been inputted')
res.redirect('/')
})
router.get('/trackingnumbers', (req, res) => {
res.render('trackingnumbers', {
data: {},
errors: {}
})
})
router.post('/trackingnumbers', [
check('officeNumber')
.isLength({ min:4, max:4 })
.withMessage('That office Number doesn‘t look right')
.trim()
], (req, res) => {
const errors = validationResult(req)
if (!errors.isEmpty()) {
return res.render('trackingnumbers', {
data: req.body,
errors: errors.mapped()
})
}
con.connect(function(err) {
if (err) throw err;
console.log("Connected!");
var sqloffice = "INSERT INTO offices (office_Number) VALUES ('1236')";
var sqltracking = "INSERT INTO `trackingnumbers` (`tracking_Number`, `office_Number`) VALUES ('343434344334', '1236')";
con.query(sqloffice, function (err, result) {
if (err) throw err;
console.log("1 office inserted");
});
con.query(sqltracking, function (err, result) {
if (err) throw err;
console.log("1 tracking inserted");
});
});
const data = matchedData(req)
console.log('Sanitized:', data)
req.flash('success', 'Thanks this has been inputted')
res.redirect('/')
})
module.exports = router
addtrackingnumber.ejs
<div class="form-header">
<% if (Object.keys(errors).length === 0) { %>
<h2>Input Fedex Trackng Number</h2>
<% } else { %>
<h2 class="errors-heading">Oops, please correct the following:</h2>
<ul class="errors-list">
<% Object.values(errors).forEach(error => { %>
<li><%= error.msg %></li>
<% }) %>
</ul>
<% } %>
</div>
<form method="post" action="/addtrackingnumber" novalidate>
<div class="form-field <%= errors.trackngNumber ? 'form-field-invalid' : '' %>">
<label for="trackngNumber">Trackng Number</label>
<textarea class="input" id="trackngNumber" name="trackngNumber" rows="4" autofocus><%= data.trackngNumber %></textarea>
<% if (errors.trackngNumber) { %>
<div class="error"><%= errors.trackngNumber.msg %></div>
<% } %>
</div>
<div class="form-field <%= errors.officeNumber ? 'form-field-invalid' : '' %>">
<label for="officeNumber">office Number</label>
<input class="input" id="officeNumber" name="officeNumber" type="officeNumber" value="<%= data.officeNumber %>" />
<% if (errors.officeNumber) { %>
<div class="error"><%= errors.officeNumber.msg %></div>
<% } %>
</div>
<div class="form-actions">
<button class="btn" type="submit">Send</button>
</div>
</form>