0

I have a problem where request.body.email returns me undefined. I wrote this code on my controller on my client side:

 wish.controller('wishCtrl',['$scope','$http','$cookies',function($scope,$http,$cookies) {
    var user={};
    user.email = $cookies.get('cookieEmail');
    console.log(user.email);
     $http.get("http://localhost:3000/wishlist",user).success(function(data){

        $scope.wishController = data; 
        console.log(data);
    });

}]);

here I see - user.email ok so there is no problem here.

on my controller on my server side I wrote:

exports.getData = function(request, response) {
    userEmail = request.body.email;
    console.log(userEmail);
}

which writes me back undefined. to call this function I have on my server.js (on the server side)

app.get('/wishlist',wish.getData);

any idea how to fix it?

user3488862
  • 1,329
  • 2
  • 12
  • 16

2 Answers2

0

You are making a GET request. There is no request body in a GET request.

If you want to read query string data then parse request.url.

The Angular documentation for get says that the last argument must be a configuration object. So it looks like you aren't even managing to put your data in the URL either. Pass a configuration too.

$http.get("http://localhost:3000/wishlist",user, {})
Community
  • 1
  • 1
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • so I can't get this object on my server side using $http.get? – user3488862 Jul 13 '16 at 10:19
  • @user3488862 — I didn't say that. You have to (a) Make sure the data is actually in the query string and (b) read the query string and not the (non-existent) body. As I said in the answer. – Quentin Jul 13 '16 at 11:38
0

because i't get function have to pass the email on the client side this way:

$http.get("http://localhost:3000/wishlist/"+user.email)

and the path of the sever should recognize it that way:

app.get('/wishlist/:email',wish.getData);  

and int the controller that way:

userEmail = request.params.email;
console.log(userEmail);
user3488862
  • 1,329
  • 2
  • 12
  • 16