Reputation: 103
Sorry, know that such questions are popular, but i couldn't find answer there.
Have output
Cannot read property 'title' of undefined
that links to post request in this file
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;
}
});
});
Sorry for scrolling, just have such queries. Found some answers that body-parser should be used before anything, but i think i don't have such problem. There is my app file.
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;
I get data from html form
<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>
Upvotes: 0
Views: 1848
Reputation: 1995
Error handlers in express have to have 4 arguments, not 3.
Therefore your /add
route of this:
router.post('/add', function (err, req, res) {
Is actually a normal route where the first parameter is getting filled with the req
(request), the second with the res
(response), the third with the next
function. Because you named the second parameter req
you've created a confusing scenario where req
is the response, err
is the request, and res
is the next function.
Because the response doesn't have a body
property and you've named the response req
, your req.body.title
is trying to property into an undefined value (body
). Rename your parameters req, res, next
and I think you'll have better luck.
Upvotes: 1
Reputation: 109
In the get method you are referencing the title as event.title
but in the post just as title
. why?
Upvotes: 0