Samot
Samot

Reputation: 113

Angular $http.get and Express req.body is empty

I have problem with req.body with sending data using $http.get. When i change in my route in routes.js method from GET to POST, and also change in my service to $http.post, everything work fine, but with GET i can't send any data to my sever in node. Anyone has any idea ?

server.js

// modules =================================================
var express        = require('express');
var app            = express();
var mongoose       = require('mongoose');
var bodyParser     = require('body-parser');
var methodOverride = require('method-override');
var path = require('path');

// configuration ===========================================


var db = require('./config/db');

var port = process.env.PORT || 8080; 
mongoose.connect(db.url); 
app.use(express.static(__dirname + '/public')); 


app.use(bodyParser.urlencoded({ extended: true })); 
app.use(bodyParser.json()); 



// routes ==================================================
require('./app/routes')(app,__dirname);

// start app ===============================================
app.listen(port);   
console.log('Magic happens on port ' + port);   
exports = module.exports = app; 

./app/routes.js

module.exports = function(app, __dirname) {

    var Card     = require('./models/card');

    app.post('/create', function(req, res) {
        var card = new Card();

        card.polishWord = req.body.polishWord;
        card.polishDescription = req.body.polishDescription;
        card.englishWord = req.body.englishWord;
        card.englishDescription = req.body.englishDescription;
        card.category = req.body.category;

        card.save(function(err){
            if(err){
                res.send(err);
            }
            res.json({message: 'Card created'});
        });
    });

    app.get('/take', function(req, res) {
        var condition = req.body.condition || {};
        console.log(req.headers);
        console.log('______________');
        console.log(req.body);
        console.log('______________');
        /*TODO :: Czemu nie odbiera parametrow GET*/
        Card.find(condition,function(err, cards) {
            if (err)
                res.send(err);

            res.json(cards);
        });
    });



    app.get('*', function(req, res) {
        res.sendFile('/public/index.html',{"root": __dirname});
    });
};

./public/js/services/CardService ( part )

cardService.getAllCards = function(){
    return $http.get('/take',{params: {condition:{"category":"animal"}},data: {condition:{"category":"animal"}}});
};

req.headers

{ host: 'localhost:8080',
  connection: 'keep-alive',
  accept: 'application/json, text/plain, */*',
  'user-agent': 'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML,
like Gecko) Chrome/50.0.2661.102 Safari/537.36',
  referer: 'http://localhost:8080/card',
  'accept-encoding': 'gzip, deflate, sdch',
  'accept-language': 'pl-PL,pl;q=0.8,en-US;q=0.6,en;q=0.4',
  cookie: '_ga=GA1.1.1073910751.1465203314' }

.package.json ( dependencies )

  "dependencies": {
    "express": "~4.13.1",
    "mongoose": "4.4.20",
    "body-parser": "~1.15.1",
    "method-override": "~2.0.2"
  },

Anyone has an idea ?

Upvotes: 1

Views: 929

Answers (1)

gnerkus
gnerkus

Reputation: 12019

req.body was undefined because you issued a 'GET' request from $http.get('/take'). What you sent to the server are query parameters. An example of query parameters would be:

http://stackoverflow.com/questions/tagged/npm?filter=all&sort=active

where filter and sort are the query parameters with the values 'all' and 'active' respectively.

To access query parameters in an Express server, you'll need to use the req.query object. It contains the query parameters and their corresponding values. The req.query object for the request to '/take' would look like this:

{
  condition: {
    category: "animal"
  }
}

Upvotes: 1

Related Questions