allencoded
allencoded

Reputation: 7275

Cannot get the values out of my get in factory

I have verified that the api is indeed returning the response but I am confused on how to retrieve that response. What am I missing?

Here is my Controller:

app.controller('valuesCtrl', function ($scope, valuesFactory) {
    console.log(valuesFactory.values())
});

Here is my service:

app.factory('valuesFactory', function ($http, $q, $cookieStore) {
    var urlBase = '/api/Values';
    var authHeader = {'Authorization' : 'bearer ' + $cookieStore.get('token') }
    var factory = {};

    factory.values = function () {
        return $http.get(urlBase, { headers: authHeader }).success(function (response) {
            return response;
        }); 
    }

    return factory;
});

Here is what my console log returns: enter image description here

Upvotes: 1

Views: 47

Answers (3)

Lukas
Lukas

Reputation: 3235

Good idea on a factory, but I would change it to return the promise from $http.get rather than values and allow the controller to bind directly to that promise. So your solution simplifies to:

Factory

app.factory('valuesFactory', function ($http, $q, $cookieStore) {
    var urlBase = '/api/Values';
    var authHeader = {'Authorization' : 'bearer ' + $cookieStore.get('token') }
    var factory = {
        fetchValues: function() {
            return $http.get(urlBase, { headers: authHeader });
        }
    }

    return factory;
});

Controller

app.controller('valuesCtrl', function ($scope, valuesFactory) {

    valuesFactory.fetchValues().then(function (data) {
        $scope.values = data;
    }, function (error) {
        $scope.error = 'Contact Support';
    });
});

Your current solution is double promising and you can skim it down to just one with the same results.

Upvotes: 2

allencoded
allencoded

Reputation: 7275

Changed my code to the following to make it work...

Factory:

app.factory('valuesFactory', function ($http, $q, $cookieStore) {
    var urlBase = '/api/Values';
    var authHeader = {'Authorization' : 'bearer ' + $cookieStore.get('token') }
    var factory = {};

    factory.values = function () {
        return $http.get(urlBase, { headers: authHeader })
           .then(function (response) {
               return response.data;
           }, function (response) {
               return $q.reject(response.data)
           });
    }

    return factory;
});

Controller:

app.controller('valuesCtrl', function ($scope, valuesFactory) {
    valuesFactory.values()
        .then(function (data) {
            $scope.values = data;
        }, function (error) {
            $scope.error = 'Contact Support';
        });
});

Upvotes: -1

Thilo
Thilo

Reputation: 262514

 .success(function (response) {
        return response;
  }); 

That won't work too well.

The success callback is supposed to do something with the response, for example storing it in your model in some way.

The callback has to accept and process the data. Its return value is disregarded (the callback is not called from your code, but asynchronously from Angular when the request is done, and Angular itself has no idea what you want to do with the response).

Upvotes: 1

Related Questions