0

So I've tried the suggestions from

But I'm still getting an empty req.body

I can do a get just fine and even pass an id for the get. But I can't seem to POST and transfer the data to the server.

Controller:

.controller("createEventController", ["$scope", 'CreateEventService', '$location', function($scope, CreateEventService, $location){
    $scope.event = {};

    $scope.submitEvent = function(){
        console.log($scope.event);
        var events = new CreateEventService($scope.event);

        console.log(events);

        events.save(function(response){
            console.log(response);
        }, function(error){
            console.log(error);
        });

    }
}])

Service

factory('CreateEventService', function($resource){      
   return $resource('api/createEvent');
});
Community
  • 1
  • 1
Chauncey Philpot
  • 319
  • 1
  • 3
  • 14

3 Answers3

2

Change events.save(...) to events.$save(...)

Kousha
  • 32,871
  • 51
  • 172
  • 296
1

You should use bodyParser.json(). The $resource, by default sends his requests with the header "Content-Type": "application/json;charset=UTF-8" and without bodyParser.json() you're API is not able to deal with the request content.

Victor Queiroz
  • 123
  • 1
  • 10
0

if using express 4.1.2, despite it fussing about bodyParser() being deprecated, having

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

was for some reason, giving me an empty body. For whatever reason, not having urlencoded crashed my server before (old express version I think). Just change it back to (using express 4.1.2)

app.use(bodyParser());

and fixed. I also removed the methodOverride() as that was causing my server to hang. If anyone knows a more correct approach, I would appreciate it.

Chauncey Philpot
  • 319
  • 1
  • 3
  • 14