Cannot read property 'finally' of undefine

2019-09-03 16:42发布

问题:

I have a rest service defined in my project which acceps the url and callback function as a parameter.

RestApiService

    service.requestFromApi = function(url, cb) {
        return service.request(config.ApiEndpoint.Base + url, cb);
    };

    service.request = function(url, cb) {
    if(typeof cb !== 'function') {
        throw new Error('Function expected');
    }

    $http.get(url).then(function(response) {
        if (response.status === 200) {
            if (typeof response.data === 'object') {
                return cb(response.data);
            }
        }
    }, function(response) {
        $window.console.error('Requesting ' + url + ' from server failed (' + response.status + ' ' + response.statusText + ')');
    });
};

I make a call to this service using:

     service.watch = function () {
        restApiService.requestFromApi('/crossing' + '?latitude=' + latestCoords.latitude + '&longitude=' + latestCoords.longitude + '&radius=' + config.radiusInMetres, updateHandler)
        .finally(function() {
            $timeout(service.watch, 5000);
        });
    };


    var processData = function (item) {
        crossing[item.crossingName] = item;
    };

    var updateHandler = function (json) {
        if (json.events && Array.isArray(json.events)) {
            json = json.events;
        } else {
            $log.warn('Smissing events field');
        }

        if (json.length > 0) {
            angular.forEach(json, processData);
        }
    };

To ensure that the next request only after the previous one is finished I have used the finally block. However I am getting the error Cannot read property 'finally' of undefined.. I have a hint that this is about some return value but I am not sure what is the exact nature of the problem.