0

I have the following controller :

app.controller('ListeSASController', function($scope, $rootScope, $routeParams, $location, userService, RefreshSASServices, $timeout){
        this.IsUserLogged = function()
        {
            return userService.user().isLogged;
        };

        var promise = $timeout(RefreshSASServices.RafraichirSAS(), 100);

        this.getSAS = function(){
            return RefreshSASServices.getSAS();
        };

        $scope.$on('$locationChangeStart', function(){
            RefreshSASServices.ArreterLesRafraichissements();
        });
});

with the following service :

app.service('RefreshSASServices', function($http, userService, serverConfigService, $q, $timeout, $translate, constantsServices) {
    var listeSAS = [];
    var $this = this;

    var promiseRefreshSAS;

    // Getters
    this.getSAS = function()
    {
        return listeSAS;
    };


    //Setters
    this.clearDatas = function()
    {
        listeSAS = [];
    };


    // Communication with the server
    $this.getServerUri = function()
    {
        return serverConfigService.getServerUri()+"majsvc/";
    };




    // Fonctions de rafraichissement
    $this.ArreterLesRafraichissements = function()
    {
        if(promiseRefreshSAS !== undefined)
            $timeout.cancel(promiseRefreshSAS);
    };

    $this.GetSASFromServer = function()
    {
        var promises;
        if(userService.user().isLogged)
        {
            var uri = $this.getServerUri() + "getAllSAS/"+userService.user().UserObject._id;
            promises = $http.get(uri)
            .success(function(data, status, headers, config) {
                // this callback will be called asynchronously
                // when the response is available
                return data;
            }).
            error(function(data, status, headers, config) {
                // called asynchronously if an error occurs
                // or server returns response with an error status.
                return "";
            });
        }else{
            promises = $q.when(!userService.user().isLogged)
        }
        return promises;    
    };
    $this.RafraichirSAS = function () {
    // functions that call
            $this.GetSASFromServer()
            .then(function(promise){

                if(promise !== undefined && promise.data !== undefined)
                {
                    listeSAS = promise.data;
                    //alert('refreshing the SAS list:' + JSON.stringify(listeSAS));

                }else listeSAS = [];
                promiseRefreshSAS = $timeout($this.RafraichirSAS, 3000);
            })
            .catch(function(error)
            {
                console.error("Error :", error);
                promiseRefreshSAS = $timeout($this.RafraichirSAS, 7000);
            });
    };





});

When I load my page using routes :

.when('/listeSAS', {
                templateUrl : './includes/sas/liste_sas.html',
                controller  : 'ListeSASController',
                controllerAs : 'controller'
            })

everything works fine, if my data changes on the server it gets updated on the UI, My UI is also displaying what I want. Everything is OK except that when the pages loads I get the following error :

TypeError: undefined is not a function
    at file:///includes/libs/angular.js:14305:28
    at completeOutstandingRequest (file:///includes/libs/angular.js:4397:10)
    at file:////includes/libs/angular.js:4705:7 

which is the function "timeout" of angular, and the line 14305 is :

try {
          deferred.resolve(fn());
        } catch(e) {
          deferred.reject(e);
          $exceptionHandler(e);
        }
        finally {
          delete deferreds[promise.$$timeoutId];
        }

Why angular is throwing this exception ? What did I do wrong ? To be known : On my login page I set 2 timeouts which I don't stop because they refresh "global" variables such as the number of private messages. Despite the error both timeout are still working.

I use node webkit with my application and it crashes maybe one in three times when I open this route (after 5-10 seconds).

Thank you for your help.

1 Answer 1

1

Is it that you're calling RafraichirSAS(), which returns undefined instead of passing in the function?

E.g, instead of

$timeout(RefreshSASServices.RafraichirSAS(), 100);

Do

$timeout(RefreshSASServices.RafraichirSAS, 100);
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.