1

I'm using angularjs 1.5.8.

I get this error when I'm trying to cancel an http request with angular :

$cancelRequest is not a function

My code :

app.factory('User', function($resource) {
    var getUsersResource = $resource(
        '/users',
        null,
        {get : {method: 'GET', isArray: true, cancellable: true}}
    );

    return {
        getUsers : function() {
            return getUsersResource.get({},
                function(data) {
                    ...
                }, function(error) {
                    ...
                }
            );
        }
    };
});

app.controller('InitController', function($rootScope, User, ...) {
    ...
    User.getUsers();
    ...
}

app.factory('AuthInterceptor', function($q, $location, $injector) {
    return {
        responseError: function(response) {
            if (response.status === 401) {
                $injector.get('$http').pendingRequests.forEach(
                    function (pendingReq) {
                        pendingReq.$cancelRequest();
                    }
                );
                $location.path('login');
            }
            return $q.reject(response);
        }
    };
});

Do you know how I can solve this error ?

Thanks

Nan
  • 63
  • 1
  • 9

2 Answers2

0

The documentation suggests that $cancelRequest should be used with the resource object. From my initial review, it appears that you're correctly using $resource within the User factory. But, I'm not sure about how you're implementing this within the AuthInterceptor factory. It doesn't look like you're using User.getUsersSources() at all. Therefore, I believe the reason that you're getting that error is because you're not using $cancelRequestion correctly. That being said, you might have forgotten to include other parts of the code.

Ideally, the resolved $resource object from User.getUserResources() should be passed into AuthInteceptor.

richdotjs
  • 204
  • 1
  • 2
  • 10
  • When I do User.getUsersResource() I get this object with no $cancelRequestion method `Array[0] $promise: d $resolved: false length: 0 __proto__: Array[0]` – Nan Nov 04 '16 at 14:59
  • Can you give an example of your solution please? – Nan Nov 04 '16 at 15:38
  • I don't have an example. Simply going off what I read from the documentation: https://docs.angularjs.org/api/ngResource/service/$resource – richdotjs Nov 04 '16 at 18:29
  • Could you update your question with code snippets where you are invoking User.getUserResources()? That console.log() doesn't provide a full picture. Did you look at that object's _proto_ ? It's highly possible that the $cancelRequestion resides within _proto_ – richdotjs Nov 04 '16 at 18:32
  • I haven't any $cancelRequestion method inside proto. Of course I have seen before the documation but it doesn't help me. – Nan Nov 07 '16 at 12:11
  • Hi, did you solve this issue? I got the same error, tried several variant define factory/service etc, but the same **TypeError: TestServiceTop.test(...).$cancelRequest is not a function** `I update to angular v1.6.6 and angular-ui-router v1.0.11` please suggest any help... – Victor Orletchi Nov 27 '17 at 08:18
0

I think that you should declare your service like that:

.factory('categoryService', ['$resource', function($resource) {
return $resource('/', {},
    {
        'get': {
            'method': 'GET',
            'cancellable': true,
            'url': '/service/categories/get_by_store.json',
        },
    });
}])

And when you use this service, it should be called so:

if ( $scope.requestCategories ) {
    $scope.requestCategories.$cancelRequest();
}
$scope.requestCategories = categoryService['get']({

}, function(res){
    //some here
}, function(err){
    //some here           
}); 
ray
  • 171
  • 1
  • 11