我想在数据库中添加一个快速路线html表单的代码。表单正在工作,经过清理的跟踪和办公室号码已从用户输入正确输出到控制台,但对数据库的调用未完成。
你知道为什么从快线发起邮递时没有呼叫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' }
服务器.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
添加跟踪编号.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>
暂无答案!
目前还没有任何答案,快来回答吧!