我是一名新手程序员。我一直在制作一个非常小的网站。我遇到了一点错误。我找到了一些解决方案。但我还没有得到答案。
如果你不介意,请给我你的tohgt。 T^T
我将 node.js 与 Mysql 连接起来。 “GET”运行没有任何问题。但是“POST”部分无法获取我在网页上键入的一些数据。
我想知道的是为什么“POST”不获取数据。
var fs = require('fs');
var ejs = require('ejs');
var http = require('http');
var mysql = require('mysql');
var express = require('express');
var client = mysql.createConnection({
user: 'root',
password: 'PASSWORD',
database: 'Company'
});
var app = express();
http.createServer(app).listen(8080, function(){
console.log('Server running at http://127.0.0.1:8080');
});
app.get('/', function(request, response) {
fs.readFile('list.html', 'utf8', function(error, data) {
client.query('SELECT * FROM products', function (error, results) {
response.send(ejs.render(data, {
data: results
}));
});
});
});
app.get('/delete/:id', function(request, response) {
client.query('DELETE FROM products WHERE id=?', [request.param('id')], function() {
response.redirect('/');
});
});
app.get('/insert', function(request, response) {
fs.readFile('insert.html', 'utf8', function (error, data) {
response.send(data);
});
});
app.post('/insert', function(request, response) {
var body = request.body;
client.query('INSERT INTO products (name, modelnumber, series) VALUES (?, ?, ?)'[
body.name, body.modelnumber, body.series
], function() {
response.redirect('/');
});
});
app.get('/edit/:id', function(request, response) {
fs.readFile('edit.html', 'utf8', function(error, data) {
client.query('SELECT * FROM products WHERE id = ?', [
request.param('id')
], function(error, result) {
response.send(ejs.render(data, {
data: result[0]
}));
});
});
});
app.post('/edit/:id', function(request, response) {
var body = request.body;
client.query('UPDATE products SET name=?, modelnumber=?, series=? WHERE id=?', [
body.name, body.modelnumber, body.series, request.param('id')
], function() {
response.redirect('/');
});
});
错误信息
TypeError: Cannot read property 'name' of undefined at app.get.fs.readFile.client.query.response.send.ejs.render.data (/home/han/app.js:46:9) at callbacks (/home/han/node_modules/express/lib/router/index.js:164:37) at param (/home/han/node_modules/express/lib/router/index.js:138:11) at pass (/home/han/node_modules/express/lib/router/index.js:145:5) at Router._dispatch (/home/han/node_modules/express/lib/router/index.js:173:5) at Object.router (/home/han/node_modules/express/lib/router/index.js:33:10) at next (/home/han/node_modules/express/node_modules/connect/lib/proto.js:193:15) at Object.expressInit [as handle] (/home/han/node_modules/express/lib/middleware.js:30:5) at next (/home/han/node_modules/express/node_modules/connect/lib/proto.js:193:15) at Object.query [as handle] (/home/han/node_modules/express/node_modules/connect/lib/middleware/query.js:45:5)
当我尝试将数组值更改为“值”app.post('/insert') 部分时,网页显示“值”。
最佳答案
错误是request.body 未定义。它是未定义的,因为您还没有配置 Express 来解析正文。为此,您需要安装模块 body-parser
npm install body-parser
接下来,在你的代码中要求 body-parse
var bodyParser = require('body-parser')
并在初始化 Express 后将其设置为中间件。
var app = express();
app.use(bodyParser.urlencoded({ extended: false }));
您的 request.body 现在应该等于表单。
编辑1:
为你的插入查询试试这个
app.post('/insert', function(request, response) {
var body = request.body;
var product = {
name: body.name,
modelnumber: body.modelnumber,
series: body.series
}
client.query('INSERT INTO products SET ?', product, function() {
response.redirect('/');
});
});
关于mysql - 如何在 node.js 中使用 token(post) Mysql?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28325955/