Austin Austin -4 years ago 73
AngularJS Question

NodeJS Express Req.Body is undefined

I have worked with several solutions posted on stack overflow, and I have yet to find one to work with me so I decided to ask.

Here is my nodejs file: (leaving out irrelevant code)



var express = require('express');
var app = express();
var cors = require('cors');
var bodyParser = require('body-parser');

app.use(bodyParser.urlencoded());
app.use(bodyParser.json());
app.use(cors());

app.get('/sendEmail', function(req, res) {
var answers = req.body.answers;
console.log(answers);
})





and also my POST:



$http.get('http://localhost:5000/sendEmail', {
answers: answer
})





How come when I console log
req.body.answers
I get undefined?

Answer Source

You cannot have a body in a GET request. So change this:

app.post('/sendEmail', function(req, res) {
    var answers = req.body.answers;
    // whatever
})

and this:

$http.post('http://localhost:5000/sendEmail', {
  answers: answer
})
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download