2

I have written a custom method to do a call to my API passing some extra parameters, but I get the following error: TypeError test.testing is not a function. I followed the code in here: https://docs.angularjs.org/api/ngResource/service/$resource

This is the code I use, $save for example is working fine.

I am using version 1.4.1.

services.factory('Calendar',function($resource){
    return $resource('/api/calendar/:id',{
        update: {
            method: 'PUT'
        },
        testing: {
            method: "POST", params:{charge:true}
        }
    });
});

function( $scope, Calendar ) {

        var test = new Calendar();
        test.title = "hello";
        test.$testing();
        ...
}
6
  • Have you tried test.$testing() Commented Jun 19, 2015 at 13:36
  • Yes I did, it was my initial script, changed it to "testing" afterwards which is also not working. Commented Jun 19, 2015 at 13:40
  • I don't think that you've completely followed the example... Commented Jun 19, 2015 at 13:47
  • This is what I would think link Commented Jun 19, 2015 at 13:52
  • Thx, it works, because you have added {id: 200} but why isn't it working when I leave it out? Commented Jun 19, 2015 at 14:04

1 Answer 1

1

The reason is because you haven't quite followed the example in the docs. See:

angular.module('testApp', [])
  .factory('CreditCard', creditCard)
  .factory('Calendar', calendar);

function creditCard() {
  return $resource('/user/:userId/card/:cardId',
      {userId:123, cardId:'@id'}, {
      charge: {method:'POST', params:{charge:true}}
  });
}

function calendar() {  
  return $resource('/api/calendar/:id',
    {id: 200},
    {
      update: {
          method: 'PUT'
      },
      testing: {
          method: "POST", params:{charge:true}
      }
  });
}

As you can see I added that second object argument {id:200} that tells the ng-resource that in order to fill out /api/calendar/:id correctly it needs to pull the property id from that object and use its value.

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.