我有 属性 名称,但 req.body.name1 抛出未定义
I have the property name but req.body.name1 throws undefined
我查看了与相同问题相关的其他答案,但我无法弄明白。有人可以帮助我吗?
这是我的 main.js 文件:
app.get('/addbook', function (req,res){
res.render('addbook.html');
});
app.post('/bookadded', function (req,res) {
// saving data in database
let sqlquery = "INSERT INTO books (name, price) VALUES (?,?)";
// execute sql query
let newrecord = [req.body.name1, req.body.price];
db.query(sqlquery, newrecord, (err, result) => {
if (err) {
return console.error(err.message);
}
else
res.send(' This book is added to database, name: '+ req.body.name + ' price '+ req.body.price);
});
这是我的 addbook 文件的主体:
<!doctype html>
<html>
<head>
<title>This is the addbook page</title>
</head>
<h1> This is the add book page </h1>
<body>
<p>Please fill related data to add the book to database: </p>
<form action="/bookadded" method="POST">
<input type="text" name="name1" />
<input type="text" name="price" />
<input type="submit" />
</form>
</body>
</html>
这是错误:
TypeError: 无法读取未定义的 属性 'name1'
在 /home/cmesa001/lab3/routes/main.js:41:37
这是index.js:
var bodyParser = require ('body-parser')
var express = require ('express')
const mysql = require('mysql');
const app = express()
const port = 8000
const db = mysql.createConnection ({
host: 'localhost',
user: 'root',
password: 'descarcare',
database: 'myBookshop'
});
// connect to database
db.connect((err) => {
if (err) {
throw err;
}
console.log('Connected to database');
});
global.db = db;
// new code added to your Express web server
require('./routes/main')(app);
app.use(bodyParser.urlencoded({ extended: true }))
app.set('views',__dirname + '/views');
app.set('view engine', 'ejs');
app.engine('html', require('ejs').renderFile);
app.listen(port, () => console.log(`Example app listening on port ${port}!`))
network tab on developer tools
在将路线附加到 express 之前,将代码中的 app.use(bodyParse.urlencoded({ extended: true }))
上移。添加中间件和东西的顺序来表达问题。我通常先添加头盔,然后是 bodyParser、compression、morgan,然后再添加这些路由。
您的输入名为 "name1",您的服务器需要 "name"
更改这个:
res.send(' This book is added to database, name: '+ req.body.name + ' price '+ req.body.price);
为此:
res.send(' This book is added to database, name: '+ req.body.name1 + ' price '+ req.body.price);
我查看了与相同问题相关的其他答案,但我无法弄明白。有人可以帮助我吗?
这是我的 main.js 文件:
app.get('/addbook', function (req,res){
res.render('addbook.html');
});
app.post('/bookadded', function (req,res) {
// saving data in database
let sqlquery = "INSERT INTO books (name, price) VALUES (?,?)";
// execute sql query
let newrecord = [req.body.name1, req.body.price];
db.query(sqlquery, newrecord, (err, result) => {
if (err) {
return console.error(err.message);
}
else
res.send(' This book is added to database, name: '+ req.body.name + ' price '+ req.body.price);
});
这是我的 addbook 文件的主体:
<!doctype html>
<html>
<head>
<title>This is the addbook page</title>
</head>
<h1> This is the add book page </h1>
<body>
<p>Please fill related data to add the book to database: </p>
<form action="/bookadded" method="POST">
<input type="text" name="name1" />
<input type="text" name="price" />
<input type="submit" />
</form>
</body>
</html>
这是错误:
TypeError: 无法读取未定义的 属性 'name1' 在 /home/cmesa001/lab3/routes/main.js:41:37
这是index.js:
var bodyParser = require ('body-parser')
var express = require ('express')
const mysql = require('mysql');
const app = express()
const port = 8000
const db = mysql.createConnection ({
host: 'localhost',
user: 'root',
password: 'descarcare',
database: 'myBookshop'
});
// connect to database
db.connect((err) => {
if (err) {
throw err;
}
console.log('Connected to database');
});
global.db = db;
// new code added to your Express web server
require('./routes/main')(app);
app.use(bodyParser.urlencoded({ extended: true }))
app.set('views',__dirname + '/views');
app.set('view engine', 'ejs');
app.engine('html', require('ejs').renderFile);
app.listen(port, () => console.log(`Example app listening on port ${port}!`))
network tab on developer tools
在将路线附加到 express 之前,将代码中的 app.use(bodyParse.urlencoded({ extended: true }))
上移。添加中间件和东西的顺序来表达问题。我通常先添加头盔,然后是 bodyParser、compression、morgan,然后再添加这些路由。
您的输入名为 "name1",您的服务器需要 "name"
更改这个:
res.send(' This book is added to database, name: '+ req.body.name + ' price '+ req.body.price);
为此:
res.send(' This book is added to database, name: '+ req.body.name1 + ' price '+ req.body.price);