0

Hello: I'm making a long polling with angular js and everything works until I make a getResponse function the program involves a) a main (at the bottom) b) a constructor to a service c) "the famous" getResponse which is a function of the service d) A simple console log to process response e) A setPolling service which is the loop f) Set response which makes a response to the service g) A simple directive

Can you tell why is throwing this.getResponse is not a function when actually is... ? heres the code

var DbService = function($http, $window, $interval) {
    this.http_ = $http;
    this.interval_ = $interval;
    this.window_ = $window;
    this.db = null;
};

DbService.prototype.getResponse = function() {
    this.http_({
        method: 'get',
        url: 'dependencies/request.php',
        data: ""
        }).then(function successCallback(response) {
            this.setResponse(response);
        }, function errorCallback(response) {
            console.log("Err response");
    });
};

DbService.processResponse = function(response) {
    console.log(this.response_);
};

DbService.prototype.setPolling = function() {
    this.interval_(function(){
        console.log("Hi World!");
        this.response_ = this.getResponse();
        this.processResponse(this.response_);
    }, 10000);
};

DbService.prototype.setResponse = function(response) {
    this.response_ = response;
};

var informAction = function($scope, DbService) {
    return {
        restrict: 'C',
        link: function($scope, elem, attr, ctrl) {
            action = DbService.getResponse();
            $scope.consmensaje += "Accion: " + action.data['action'] + "\n";
        }
    };
};

var MainController = function($http, $window, $interval, DbService) {
    this.http_ = $http;
    this.window_ = $window;
    this.interval_ = $interval;
    this.DbService_ = DbService;
    console.log("Dentro de Controller");
    this.DbService_.setPolling();
};


function main() {
    console.log("Comenzando");
    var app = angular.module('consola', []);
    app.service('DbService', DbService);
    app.controller(
      'MainController',
      ['$scope', '$http', '$interval', 'DbService',
        MainController]);
    app.directive('informAction', ['$scope', 'DbService', informAction]);
}

main();
Gabriel
  • 13
  • 3
  • `this` inside the promise callbacks isn't what you think it is – charlietfl May 22 '16 at 03:36
  • Where exactly is your error? In javascript you always have to be sure what `this` actually refers to in terms of scope. Often times when this type of error occurs, the programmer has overlooked what exactly `this` refers to – element11 May 22 '16 at 03:36
  • you were right. I get rid of the this and put the DbService instead and now throws undefined becase has nothing to get I think – Gabriel May 22 '16 at 03:52
  • One thing to understand about javascript is that the 'this' object is when you move inside of a method the reference changes to be that object. In order to keep the outer object reference it is common for people to create another reference to the outer reference by using 'that = this'; This may be confusing and I don't have space to write it here... so here's an article to help: http://stackoverflow.com/questions/4886632/what-does-var-that-this-mean-in-javascript – Dale May 22 '16 at 03:56
  • This is one of highest linked javascript posts on this site by a Facebook engineer.... http://stackoverflow.com/questions/20279484/how-to-access-the-correct-this-context-inside-a-callback – charlietfl May 22 '16 at 10:49

0 Answers0