Gabriel Matusevich
Gabriel Matusevich

Reputation: 3855

Angularjs $http then function

I've created a JS Object with a method that calls a $http to retrieve a value, but when the $http is done I want to assign this value to a property, I can't seem to be able to get this value:

the property this.user always ends up with the promise itself, but I want to assign the value returned from the XHR Request or undefined on failure, I think this is a context problem, I just don't know how to fix it

var Ticket = function(numint, company_id, user_id, title, description, priority, status, assignation, created_at) {
            this.numint         = numint;
            this.company_id     = company_id;
            this.user_id        = user_id;
            this.title          = title;
            this.description    = description;
            this.priority       = priority;
            this.status         = status;
            this.assignation    = assignation;
            this.created_at     = created_at;

            this.user           = undefined;

            this.getUser = function() {

                if(this.user_id === undefined)
                    return false;

                var http = 
                    $http({
                        method  : 'GET',
                        url     : '/users/' + this.user_id,
                        timeout : 100000,
                        headers : {'Content-Type': 'application/x-www-form-urlencoded'}
                    });

                this.user = http
                    .then(
                        function(data) {
                            return data;
                        }
                        , 
                        function() {
                            return undefined;
                        });

                return http;

            }
        };

Upvotes: 0

Views: 393

Answers (3)

Danny Blue
Danny Blue

Reputation: 463

assign this to a different value OR! or us .bind.

$http({
  method  : 'GET',
  url     : '/users/' + this.user_id,
  timeout : 100000,
  headers : {'Content-Type': 'application/x-www-form-urlencoded'}
}).then(function (data){
  this.user = data;
}.bind(this));

Upvotes: 0

Morgan Polotan
Morgan Polotan

Reputation: 223

var http is a promise object because the return value of Angular's $http service is a promise (docs). Use .then() to get the return value once the AJAX request has returned and the promise has resolved.

var self = this;

http.then(function (data) {
  self.user = data;
});

Upvotes: 2

bto.rdz
bto.rdz

Reputation: 6720

var Ticket = function(numint, company_id, user_id, title, description, priority, status, assignation, created_at) {
        var self = this; // new code
        self.numint         = numint; //use self inseat
        self.company_id     = company_id;

        this.getUser = function() {

            if(self.user_id === undefined) // you had one preblem here, because "this" here is direrent to the this.user_id you neded, so use self
                return false;

            var http = 
                $http({
                    method  : 'GET',
                    url     : '/users/' + this.user_id,
                    timeout : 100000,
                    headers : {'Content-Type': 'application/x-www-form-urlencoded'}
                }).then(function (data){
                      self.user = data
                   ;},
                 function () {
                  self.user= undefined;
                   });


        }
    };

Upvotes: 0

Related Questions