很抱歉,知道这些问题很受欢迎,但我无法在那里找到答案。
有输出
无法读取属性'标题'未定义的
链接到此文件中的帖子请求
const express = require('express');
const router = express.Router();
router.get('/', function (req, res, next) {
let query = 'select event.id, event.title, event.description, event.date, place.title as place, organisator.title as organisator, subcategory.title as subcategory from ((event inner join place on event.place_id = place.id) inner join organisator on event.organisator_id = organisator.id) inner join subcategory on event.subcategory_id = subcategory.id';
res.locals.connection.query(query, function (error, results, fields) {
if (error) throw error;
res.render('index', { title: 'Events', data: results });
});
});
router.post('/add', function (err, req, res) {
const query = "insert into event (title, description, event.date, subcategory_id, place_id, organisator_id) values ('" + req.body.title + "','" + req.body.description + "','" + req.body.date + "',(SELECT id from subcategory WHERE title = '" + req.body.subcategory + "'),(SELECT id from place WHERE title = '" + req.body.place + "'),(SELECT id from organisator WHERE title = '" + req.body.organisator + "'))";
connection.query(query, function (err) {
if (err) {
console.error(err);
throw err;
}
});
});
抱歉滚动,只是有这样的疑问。 找到一些答案,身体解析器应该在任何事情之前使用,但我想我不会有这样的问题。有我的app文件。
const express = require('express');
const path = require('path');
const logger = require('morgan');
const bodyParser = require('body-parser');
var app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(logger('dev'));
app.use(express.static(path.join(__dirname, 'public')));
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
var mysql = require("mysql");
app.use(function(req, res, next){
res.locals.connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : 'Strange3865hum31315',
database : 'minskevents'
});
res.locals.connection.connect();
next();
});
app.use('/', require('./routes/index'));
app.use('/events', require('./routes/events'));
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
app.use(function(err, req, res, next) {
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
res.status(err.status || 500);
res.render('error');
});
var http = require('http');
var server = http.createServer(app);
server.listen(4000);
if(server.listening){
console.log('Server is listening on port 4000');
}
module.exports = app;
我从html表格中获取数据
<form action="/events/add" method="POST">
<input type="text" name="title" placeholder="Event name" />
<input type="text" name="subcategory" placeholder="Category" />
<input type="text" name="date" placeholder="Date" />
<input type="text" name="place" placeholder="Place" />
<input type="text" name="organisator" placeholder="Organisator" />
<textarea name="description" placeholder="Description" onkeyup="adjust_textarea(this)"></textarea>
<input type="submit" value="Add" />
</form>
答案 0 :(得分:1)
express中的错误处理程序必须有4 arguments, not 3。
因此,您的/add
路线:
router.post('/add', function (err, req, res) {
实际上是第一个参数填充req
(请求)的常规路由,第二个参数填充res
(响应),第三个参数填充next
函数。由于您将第二个参数命名为req
,因此您创建了一个令人困惑的方案,其中req
是响应,err
是请求,res
是下一个功能。
由于回复没有body
属性且您已将回复命名为req
,因此您的req.body.title
正在尝试将属性设置为未定义的值({ {1}})。重命名您的参数body
,我认为您会有更好的运气。
答案 1 :(得分:0)
在get方法中,您将标题引用为event.title
,但在帖子中引用为title
。为什么呢?