0

I'm trying to using the $resource library of angular, to POST data to a nested resource.

My following nested resource of event looks like so

  • events/:eventId/match - POST
  • events/:eventId/match - GET
  • events/:eventId/match/:matchId - GET

I set up a service with angular

app.factory('EventService', ['$resource', function ($resource) {

            var Event = $resource('/events/:eventId', {eventId: '@id'},
                          {
                            createMatches: {
                               url: '/events/:eventId/match',
                               method: 'POST'
                            }
                        );

            return {

                 createMatches: function(data,event_id) {

                      var data.eventId = event_id;
                      return Event.createMatches(data).$promise;

                 } 
            }

});

Controller where it has been called:

app.controller('EventController', ['$scope','EventService', function($scope,EventService) {

    $scope.create = function(event_id,title,description) {

        EventService.createMatches({
            title:title,
            description: description
        },event_id).then(function(result) {

            console.log('event_created', result);
        })
    }

}]);

Problem

When I send the request to the server I expect the url that looks like so: /events/10/match

But instead the resource doesn't add the eventId as a parameter of the url but add it as a parameter of the request, for this reason my call fail because the url looks like so: /events/match.

I can't understand why it doesn't bind the :eventId to the url. Any suggest will be appreciated.

4
  • share the code how you calling this factory? Commented Jan 10, 2015 at 17:09
  • Done, I update the question with the function where I call the factory Commented Jan 10, 2015 at 17:14
  • does the object have a property id ? Commented Jan 10, 2015 at 17:25
  • what version of angular are you using? Commented Jan 10, 2015 at 17:51

1 Answer 1

0

I believe that you are missing your second parameter decalaration, as per this link for the actions you are defining:

app.factory('EventService', ['$resource', function ($resource) {

    var Event = $resource('/events/:eventId', {eventId: '@id'},
                  {
                    createMatches: {
                       url: '/events/:eventId/match',
                       method: 'POST',
                       params: {eventId: '@id'}
                    }
                );

    return {

         createMatches: function(data,event_id) {

              var data.eventId = event_id;
              return Event.createMatches(data).$promise;

         } 
    }

});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.