在 products.js 页面中,我在 2 个不同的路由中使用了 req.body,但在一个路由中,它运行良好,但在另一个路由中它不起作用。它是未定义的。 (问题出在路线 router.post('/edit/:productID')
。)
index.js
const http = require('http')
const https = require('https')
const express = require('express')
const mysql = require('mysql')
const ejs = require('ejs')
const session = require('express-session')
const cookieparser = require('cookie-parser')
const bodyparser = require('body-parser')
const main = require('./routes/main')
const products = require('./routes/products')
const { isBuffer } = require('util')
const { LOADIPHLPAPI } = require('dns')
const { EDESTADDRREQ } = require('constants')
const { mainModule } = require('process')
const app = express()
app.use(express.static('public'))
app.use(bodyparser.urlencoded({ extended: false }))
app.use(bodyparser.json())
app.use(cookieparser())
app.use(session({
secret: 'secret',
saveUninitialized: false,
resave: false
}))
app.set('view engine', 'ejs')
app.use('/', main)
app.use('/products', products)
const db = mysql.createConnection({
host: "localhost",
user: "root",
password: "",
database: "info"
})
db.connect((err) => {
if(err)
console.log(err)
})
app.listen(3000, (err) => {
if(err) console.log(err)
})
products.js
const express = require('express')
const router = express.Router()
const mysql = require('mysql')
const app = express()
const isseller = require('../middleware/auth.js')
const db = mysql.createConnection({
host: "localhost",
user: "root",
passowrd: "",
database: "info"
})
let prodname = ""
let proddesc = ""
let prodcategpry = ""
let prodprice = ""
let userid = 0
let productID = 0
router.get('/addproduct', isseller, (req, res) => {
res.render('addproduct.ejs')
})
router.post('/addproduct', isseller, (req, res) => {
prodname = req.body.name
proddesc = req.body.desc
prodcategory = req.body.category
prodprice = parseInt(req.body.price)
userid = req.cookies.logininfo.userid
db.query('INSERT INTO products (`prodname`, `proddesc`, `prodcategory`, `prodprice`, `userid`) VALUES ("'+prodname+'", "'+proddesc+'", "'+prodcategory+'", "'+prodprice+'", "'+userid+'")', (err, result) => {
if(err)
console.log(err)
else {
res.send("success")
}
})
})
router.get('/myproducts', isseller, (req, res) => {
userid = req.cookies.logininfo.userid
db.query("SELECT * FROM products WHERE userid = ?", [userid], (err, result) => {
if(err)
console.log(err)
else
res.render('myproducts', {result: result})
})
})
router.get('/edit/:productID', isseller, (req, res) => {
productID = parseInt(req.params.productID)
db.query("SELECT * FROM products WHERE prodid = ?", productID, (err, result) => {
if(err)
console.log(err)
else
res.render('editproduct', {result: result})
})
})
router.post('/edit/:productID', isseller, (req, res) => {
prodname = req.body.name
proddesc = req.body.desc
prodcategory = req.body.category
prodprice = req.body.price
productID = req.params.productID
console.log(prodname)
db.query("UPDATE products SET prodname = '"+prodname+"' WHERE prodid = '"+productID+"'", (err, result) => {
if (err)
console.log(err)
else
res.redirect('/products/myproducts')
})
})
router.delete('/delete/:productID', isseller, (req, res) => {
db.query("DELETE FROM products prodid = ?", [req.params.productId], (err, result) => {
if(err)
console.log(err)
else
res.redirect('/products/myproducts')
})
})
module.exports = router
editproduct.ejs
<!DOCTYPE html>
<html>
<head>
<title> Title </title>
</head>
<body>
<input name="name" type="text" value=<%= result[0].prodname %>>
<br>
<input type="text" name="desc" value=<%= result[0].proddesc %>>
<br>
<input type="text" name="category" value=<%= result[0].prodcategory %>>
<br>
<input type="number" name="price" value=<%= result[0].prodprice %>>
<br>
<form action="/products/edit/<%= result[0].prodid %>" method="POST">
<button type="submit"> Submit </button>
</form>
</body>
</html>
我也使用了 app.use(express.urlencoded())
,因为我正在使用 express 5,但它仍然无法正常工作。