javascript - 500 TypeError: Cannot read property 'name' of undefined WHILE transferring data from html form to Node js server? -
i trying send data html page nodejs server .
my html code :
<body> <nav> <ul> <li> <a href="#" class="button add">add product</a> <div class="dialog" style="display:none"> <div class="title">add product</div> <form action="addevent" method="get"> <input id = "name" name="name" type="text" placeholder="product name"/> <input name="code" type="text" placeholder="product code"/> <input name="category" type="text" placeholder=" category"/> <input name="brand" type="text" placeholder="brand"/> <input type="submit" value="ok"/> </form> </div> </li> <li class="radio"> <a href="#" class="button active"></a> <a href="#" class="button"></a> <a href="#" class="button"></a> </li> </ul> </div> </nav> <p></p> <script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script> <script src="js/index.js"></script> </body>
and server code :
server.js
var express = require("express"), app = express(), bodyparser = require('body-parser'), errorhandler = require('errorhandler'), methodoverride = require('method-override'), hostname = process.env.hostname || 'localhost', port = parseint(process.env.port, 10) || 4004, publicdir = process.argv[2] || __dirname + '/public'; var exec = require('child_process').exec; var fs = require('fs'); //show homepage app.get("/", function (req, res) { res.redirect("/index.html"); }); app.get("/addevent", function (req, res){ res.send('you sent name "' + req.body.name + '".'); }); //search page app.use(methodoverride()); app.use(bodyparser.json()); app.use(bodyparser.urlencoded({ extended: true })); app.use(express.static(publicdir)); app.use(errorhandler({ dumpexceptions: true, showstack: true })); console.log("server showing %s listening @ http://%s:%s", publicdir, hostname, port); app.listen(port);
but gives following error when submit form html page !
connect 500 typeerror: cannot read property 'name' of undefined @ app.use.bodyparser.urlencoded.extended (/home/shubham/music/pricesync/server/server.js:17:45) @ layer.handle [as handle_request] (/home/shubham/music/pricesync/server/node_modules/express/lib/router/layer.js:95:5) @ next (/home/shubham/music/pricesync/server/node_modules/express/lib/router/route.js:131:13) @ route.dispatch (/home/shubham/music/pricesync/server/node_modules/express/lib/router/route.js:112:3) @ layer.handle [as handle_request] (/home/shubham/music/pricesync/server/node_modules/express/lib/router/layer.js:95:5) @ /home/shubham/music/pricesync/server/node_modules/express/lib/router/index.js:277:22 @ function.process_params (/home/shubham/music/pricesync/server/node_modules/express/lib/router/index.js:330:12) @ next (/home/shubham/music/pricesync/server/node_modules/express/lib/router/index.js:271:10) @ expressinit (/home/shubham/music/pricesync/server/node_modules/express/lib/middleware/init.js:33:5) @ layer.handle [as handle_request] (/home/shubham/music/pricesync/server/node_modules/express/lib/router/layer.js:95:5)
in http request there no req.body
that's why undefined error. values of form fields passed query string hence need read query string values req
object.
Comments
Post a Comment