0

At the angular side i have passed data using post method.

var data = {
  'id': mydata.google_id,
  'token': mydata.token,
  'email': mydata.email,
  'name': mydata.name
}; 

$http.post('http://localhost:3000/login1',data,config)
     .success(function (data, status, headers, config) { ... })
     .error(function (data, status, header, config) { ... });

At server side tried to access the data i posted using req.body.id but i couldn't

When i displayed the req.body in console i obtained the following response:

{ ' {"id" : "1234" , "email" : "xyz@gmail.com" , "name" : "xyz"}' : '  ' [_proto_] : { } }

Help me to solve this error

Sean3z
  • 3,745
  • 6
  • 26
  • 33
valarmathi
  • 35
  • 7

3 Answers3

1

Which version of express is being used? Try using for getting the request body parsed

app.use(bodyParser());
Monika
  • 31
  • 2
0

Watching at your console log, it seems that object property names are strings (note the double quote around property names).

You should set your object in this way:

var data = {
    id: mydata.google_id,
    token: mydata.token,
    email: mydata.email,
    name: mydata.name 
};

Property names are not quoted in object literal initialization (if that's not what you need :-).

See this link: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Object_initializer

Hope this helps.

Mettiu
  • 66
  • 2
  • 7
0

It looks like you may have bodyParser enabled but, you need to instead enable the bodyParser.json() middleware by using the following:

var express = require('express');
var app = express();

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

app.use(cors());

// parse application/json
app.use(bodyParser.json());

app.post('/meow', function(req, res) {
  console.dir(req.body);
  res.send(req.body);
});

app.listen(8181);

This will allow you to POST how you are already but, enable you to access req.body properties directly (vs. raw string body). Ultimately, Anuglar's $http.post() method will convert your javascript data object into JSON prior to sending it to your server.

var data = {
  id: mydata.google_id,
  token: mydata.token,
  email: mydata.email,
  name: mydata.name
};

var config = {
  headers: {}
};

$http.post('http://localhost:8181/meow', data, config);
Sean3z
  • 3,745
  • 6
  • 26
  • 33
  • thanks for the sugession. I have tried but still it is taking the body part like that only, could not resolve the problem. – valarmathi Jan 11 '16 at 05:10
  • when i call the api from postman plugin i could get the proper responce. so i think the problem is in angular side. i tried all the ways i found from google but i couldn't. – valarmathi Jan 11 '16 at 05:12
  • Do you have something in Angular that's overriding the `Content-Type` header? We have the `data` object in your post but what does the `config` object you're passing look like? – Sean3z Jan 11 '16 at 20:27
  • 'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8;' THIS S what the header am trying to include – valarmathi Jan 13 '16 at 04:48
  • @valarmathi is there any particular reason for using that `Content-Type`? Removing it from `config` would probably fix your issue (provided you continue using `bodyParser.json()`). I've added some more detail to my example, which works when testing locally – Sean3z Jan 13 '16 at 10:54