Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I've got a weird error in angularjs. I've used $resource module in angularjs to make rest requests by registering this service

$provide.service("CustomerService", ['$resource', function ($resource) {
    return $resource('/com/caspco/customers/:url:id', {}, {
        query: { method: 'GET', isArray: true, params: {id: '@id'}},
        find: { method: 'POST', isArray: true ,params:{url:'search'}},
        .......... other actions ..........
    });
}]);

in the rest server side I have a search method with the above url of find action that returns a json array. when I what to call find action by this way in the controller :

service.$find().$promise.$then(function (res) {
    console.log("resource is" + res);
}, function (error) {
    console.log("error");
});

It raises

TypeError: (anonymous function) angular.js:2070
(anonymous function) angular.js:1516
k.$apply angular.js:2575
(anonymous function) angular.js:4323
o.event.dispatch jquery.js:3
r.handle
share|improve this question
    
Any more details about the error ... –  ExpertSystem Jun 25 at 6:19
    
have you included angular-resources JS file? –  Eylen Jun 25 at 6:39
    
@Eylen Yes! I've included –  Пуя Jun 25 at 7:21
    
@Пуя: It doesn't help much to use minified version when debugging... –  ExpertSystem Jun 25 at 9:06
add comment

2 Answers

Methods on the service are not prefixed by the '$' character. Methods on resources returned from the service are prefixed by '$'. The 'then' method on the $promise property is also not prefixed by '$'.

I think if you clean up that syntax you will be on the right track.

See the angular docs.

share|improve this answer
    
Problem occurred on the response of the server –  Пуя Jun 25 at 7:19
    
And occurred only on response of the services with the array response –  Пуя Jun 25 at 7:20
add comment

First off your code to call the service should be:

service.find().$promise.then(function (res) {
  console.log("resource is" + res);
}, function (error) {
  console.log("error");
});

The next thing to check, is your response actually an array. I got caught out big time with this, I was calling a service to get a list of objects from the backend, but before returning them, the backend was wrapping them inside a Response object, so what angular received was not an array, it was an object with an array inside it.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.