Reputation: 1
I need help with node.js: I have my req.body populated like this
{
'{
"email":"[email protected]",
"password":"12345"
}'
: ''
}
but I can't get values req.body.email and req.body.password are undefined
My code is:
user.js
exports.loginByEmail= function(req, res) {
console.log('POST');
console.log(req.body);//show values
console.log(req.body.email);//undefined
console.log(req.body.password);//undefined
User.find({email:req.body.email,password:req.body.password}).toArray(function(err, userLoged) {
if(err) return res.send(500, err.message);
res.status(200).jsonp(userLoged);
});
};
app.js
var express = require("express"),
app = express(),
bodyParser = require("body-parser"),
methodOverride = require("method-override");
mongoose = require('mongoose');
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(methodOverride());
Upvotes: 0
Views: 3164
Reputation: 185
If you can not get data by using req.body.email
and req.body.password
then you should use req.query.email
and req.query.password
. This should return the data sent in a query and in this case the query was through POST method.
Hope it works !
Upvotes: 0
Reputation: 1006
The way you have the body set up in the first bit, it looks like the key is your object string and the value is an empty string.
ie your body is the key '{email:[email protected],password:12345}' (quotes removed for clarity) with the value ''
Try writing your body as
{
email: "[email protected]",
password: "12345"
}
Upvotes: 2
Reputation: 4943
Try (on the client side):
$http({
method: 'POST',
url: 'http://localhost:3000/api/loginByEmail',
data: 'email=' + encodeURIComponent(this.formLoginByEmail.username) +
'&password=' + encodeURIComponent(this.formLoginByEmail.password),
headers: {'Content-Type': 'application/x-www-form-urlencoded'}
});
Upvotes: 0