mysql - Cannot read property 'name' of undefined in Node JS -
i new in node js. i'm trying make crud app in node js using mysql. facing error can't understood or handle. please help. , thankx in adv.
i can't read input field value ejs page server.js page.
my server.js file given below
// server.js var express = require('express'); var app = express(); var bodyparser = require('body-parser'); var mysql = require('mysql'); var con = mysql.createconnection({ host: "localhost", user: "root", password: "", database: "tuts_rest" }); // set view engine ejs app.set('view engine', 'ejs'); // page app.get('/about', function(req, res) { res.render('pages/about'); }); app.post('/insert', function(req,res){ con.connect(); console.log(req.body.name); con.query("insert users(name,email,country) values('"+req.body.name+"','"+req.body.email+"','"+req.body.country+"')", function(err, results) { if (!err) res.send('inserted successfully!') else throw error; }); con.end(); }); app.listen(8080); console.log('8080 magic port');
i'm sending following data using form
<form role="form" action="/insert" method="post"> <div class="form-group"> <label for="lname">name:</label> <input type="text" class="form-control" id="name" name="name" required> </div> <div class="form-group"> <label for="email">email address:</label> <input type="email" class="form-control" id="email" name="email" required> </div> <div class="form-group"> <label for="country">country:</label> <input type="text" class="form-control" id="country" name="country"> </div> <button type="submit" class="btn btn-default">submit</button> </form>
and error given below
typeerror: cannot read property 'name' of undefined @ e:\xampp\htdocs\node\basic\server.js:40:22 @ layer.handle [as handle_request] (e:\xampp\htdocs\node\basic\node_modules\express\lib\router\layer.js:95:5) @ next (e:\xampp\htdocs\node\basic\node_modules\express\lib\router\route.js:137:13) @ route.dispatch (e:\xampp\htdocs\node\basic\node_modules\express\lib\router\route.js:112:3) @ layer.handle [as handle_request] (e:\xampp\htdocs\node\basic\node_modules\express\lib\router\layer.js:95:5) @ e:\xampp\htdocs\node\basic\node_modules\express\lib\router\index.js:281:22 @ function.process_params (e:\xampp\htdocs\node\basic\node_modules\express\lib\router\index.js:335:12) @ next (e:\xampp\htdocs\node\basic\node_modules\express\lib\router\index.js:275:10) @ expressinit (e:\xampp\htdocs\node\basic\node_modules\express\lib\middleware\init.js:40:5) @ layer.handle [as handle_request] (e:\xampp\htdocs\node\basic\node_modules\express\lib\router\layer.js:95:5)
requiring body-parser
not anything. need make express use it.
// accept url encoded app.use(bodyparser.urlencoded({ extended: true })); // accept json app.use(bodyparser.json());
note:
your queries wide open sql injection. use prepared statements or use query builder knex you.
instead of creating , managing connections one-by-one, use connection pooling. so, use
mysql.createpool
instead ofmysql.createconnection
Comments
Post a Comment