Reputation: 228
I am new in Node js. i'm trying to make CRUD app in node js using mysql. but i am facing error which i can't understood or handle. Please Help. and thankx in adv.
I can't read input field value from ejs page to server.js Page.
My server.js file is 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 the view engine to ejs
app.set('view engine', 'ejs');
// about 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 INTO 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 is the 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 is given as below
TypeError: Cannot read property 'name' of undefined
at E:\xampp\htdocs\node\basic\server.js:40:22
at Layer.handle [as handle_request] (E:\xampp\htdocs\node\basic\node_modules\express\lib\router\layer.js:95:5)
at next (E:\xampp\htdocs\node\basic\node_modules\express\lib\router\route.js:137:13)
at Route.dispatch (E:\xampp\htdocs\node\basic\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request] (E:\xampp\htdocs\node\basic\node_modules\express\lib\router\layer.js:95:5)
at E:\xampp\htdocs\node\basic\node_modules\express\lib\router\index.js:281:22
at Function.process_params (E:\xampp\htdocs\node\basic\node_modules\express\lib\router\index.js:335:12)
at next (E:\xampp\htdocs\node\basic\node_modules\express\lib\router\index.js:275:10)
at expressInit (E:\xampp\htdocs\node\basic\node_modules\express\lib\middleware\init.js:40:5)
at Layer.handle [as handle_request] (E:\xampp\htdocs\node\basic\node_modules\express\lib\router\layer.js:95:5)
Upvotes: 1
Views: 4740
Reputation: 4037
Requiring body-parser
does not do anything.
You need to make express actually use it.
// accept url encoded
app.use(bodyParser.urlencoded({
extended: true
}));
// accept json
app.use(bodyParser.json());
Note:
Your queries are wide open for sql injection. Use prepared statements or use a query builder like knex to do it for you.
Instead of creating and managing connections one-by-one, use connection pooling. To do so, usemysql.createPool
instead of mysql.createConnection
Upvotes: 3