Search code examples
javascriptangularjspolling

AngularJS Poller Throws


I'm trying to setup polling for an application, and I have an implementation that does the bare minimum where by it appears to run initially, gets the first response, but fails on to invoke $timeout and throws an exception (see below). I can't figure out why it won't poll properly it looks like it should work, the API says $timeout takes a function... Any ideas?

If I comment out line 65 $timeout(...); I don't get an error.

Exception

Error: [$http:badreq] Http request configuration must be an object.  Received: function ()
http://errors.angularjs.org/1.4.7/$http/badreq?p0=function%20()
    at angular.js:68
    at $http (angular.js:10189)
    at scoreboard-service.js:65
    at processQueue (angular.js:14745)
    at angular.js:14761
    at Scope.$eval (angular.js:15989)
    at Scope.$digest (angular.js:15800)
    at Scope.$apply (angular.js:16097)
    at done (angular.js:10546)
    at completeRequest (angular.js:10744)

The polling factory and initialization in .run.

Polling Factory

/**
 *
 */
.factory('ScoreboardPollFactory', ['ConfigSettings', 'ScoreboardFactory', '$http', '$timeout',
    function (ConfigSettings, ScoreboardFactory, $timeout) {

        var pollingTimeout = ConfigSettings.pollingTimeout;
        var data = {response: {}, calls: 0};

        var poll = function (courtId) {

            var courtId = 1; // testing app so hardcoded for now till API is ready

            console.log(ScoreboardFactory); // debug: has correct factory object, and never makes it back here after hitting $timeout

            ScoreboardFactory.getCourtScore(courtId).then(function (response) {
                data.response = response.data
                data.calls++;

                console.log(response); // debug: contains first response
                console.log(data);
                console.log(poll); // debug: outputs poll function

                $timeout(function() { // <-- this is line 65 from error
                    poll(courtId);
                }, pollingTimeout);
            });
        }

        poll();

        return {
            data: data
        };
    }])

/**
 *
 */
.run(['ScoreboardPollFactory', function (ScoreBoardPollFactory) {
}]);

Solution

  • Here's your problem (lined up so you can see what's going on)

    .factory('ScoreboardPollFactory',
                ['ConfigSettings', 'ScoreboardFactory', '$http', '$timeout',
        function (ConfigSettings,   ScoreboardFactory,   $timeout) {
    

    You're injecting the $http service as $timeout.


    I recommend using a build-tool like ng-annotate to create the dependency injection arrays so you avoid running into issues like this.