Reputation: 351
I am trying to execute an express example program for form submission using the post request . The code for app.js and index.js is as follows.
app.js
var http=require('http');
var express=require('express');
var app=express()
app.set('view engine','jade');
app.set('views','./views');
app.get('/',function(req,res){
res.render('index');
});
app.post('/signup', function(req, res) {
var name = req.body.name;
var email = req.body.email;
console.log('Name: ' + name);
console.log('Email: ' + email);
});
app.get('/search-result',function(req,res){
var name=req.query.d_name;
console.log("name is"+name)
})
http.createServer(app).listen(3000,function(){
console.log("started");
})
index.jade
html
head
title #{title}
body
h1 #{title}
p Enter the name and email id .
form(action='/search-result',method='get')
label Name
input(type='text',name='d_name')
input(type='submit',value='Search')
form(action='/signup',method='post')
div
label Name
input(type='text',name='name')
div
label Email
input(type='text',name='email')
div
input(type='submit')
In the above scenario I'm successfully able to retrieve the value in the 'd_name' text field and use console.log on using a get method ( /search-result route) by clicking search button. But when I try to use a post method(/signup route)by clicking the submit button to retrieve the value from the 'name' and 'email' text fields I am getting the following error.
"TypeError: Cannot read property 'name' of undefined"
It would be great if somebody shed some light on this.!!
Thanks in advance..
Upvotes: 1
Views: 6538
Reputation: 383
Simple use this because this built in middleware function in express
app.use(express.json());
Upvotes: 1
Reputation: 435
How to fix TypeError: Cannot read property 'name' from Express Nodemailer
app.use(express.bodyParser());
That line would let you read the post data form
Upvotes: 3