Reputation: 3009
I am not able to get the post parameters in my node js ,when I put console it says undefined but when I post parameters from postman it is working.Can anyone suggest help, please?
exports.login = function( req, res ) {
console.log(req.body)
var query = 'select * from profile where email = ? and password = ?';
connection.query(query,[req.body.email,req.body.password],function(error,result,rows,fields){
if(!!error){console.log(error)
console.log('fail');
}else{
console.log(result);
res.send(result);
}
// }
});}
My Express code,
var express = require('express')
, cors = require('cors')
, app = express();
var admin = require('../controller/user');
router.post('/login',cors(), admin.login);
Upvotes: 0
Views: 2695
Reputation: 6232
Always put body-parser
before all route. Like this
var app=express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
Other wise always get undefined body
value if you use it before any route since body not parsed yet.
Upvotes: 2
Reputation: 3309
You forgot to use bodyParser
. First install it using command
npm install body-parser --save
Then add the following to your app
var bodyParser = require('body-parser');
app.use(bodyParser.json()); // support json encoded bodies
app.use(bodyParser.urlencoded({ extended: true })); // support encoded bodies
After that you can use req.body
Upvotes: 1