1

I am new to using AngularJs and I have created a simple factory using $http get and it gets a .json that has a bunch or http status code numbers as keys, and their respective messages as values. I for some reason contine to get this error:

Cannot read property 'get' of undefined

json:

{
    "200": "Ok",
    "201": "Created",
    "202": "Accepted",
    "404": "Not_Found",
    "400": "Bad Request",
    "403": "Forbidden",
    "417": "Expectation Failed"
}

factory.js

  .factory('statusCodesFactory', function () {

                var httpStatusCodes = {
                    getStatus: function ($http) {
                        $http.get('catalog/statusCodes.json')
                            .then(function (response) {
                               httpStatusCodes.code = response;
                            });
                    }
                }
                return httpStatusCodes;
            })
user2402107
  • 913
  • 5
  • 22
  • 43

3 Answers3

5

You need to pass in the dependency for '$http' properly.

.factory('statusCodesFactory', ['$http', function ($http) {
    var httpStatusCodes = {
        getStatus: function () {
            $http.get('catalog/statusCodes.json')
                .then(function (response) {
                    httpStatusCodes.code = response;
                });
            }
        }
        return httpStatusCodes;
    });

This being said, your function is not really returning anything. A better format for it would be this:

.factory('statusCodesFactory', ['$http', function ($http) {
    var httpStatusCodes = {
        getStatus: function () {
            return $http.get('catalog/statusCodes.json')
                .then(function (response) {
                    return response;
                });
            }
        }
        return httpStatusCodes;
    });

Called like this:

var statusCodes = {};
statusCodesFactory.getStatus().then(function(response){
    statusCodes = response;
});
MBielski
  • 6,628
  • 3
  • 32
  • 43
  • followed your example and where I am calling my factory I logged console.log(statusCodesFactory.getStatus()); and it says undefined. – user2402107 May 02 '16 at 18:43
  • 1
    Without some way to resolve that promise, yes, I would expect that. Using console.log will not resolve the promise within the function. – MBielski May 02 '16 at 18:53
  • How do I get the data out of the promise? – user2402107 May 03 '16 at 16:42
  • var statusCodes outside of the promise is undefined. – user2402107 May 03 '16 at 16:46
  • I've updated the declared value for statusCodes. Until the promise resolves, it will be an empty object. Once the promise resolves it should contain the values that were returned. If you need this to be present when a controller loads on a page, I would load this list in the `.run()` section of your app as I don't see why it would change on a frequent basis. – MBielski May 03 '16 at 17:13
1

Inject $http service to your factory.

.factory('statusCodesFactory', ['$http', function ($http) {
    return {
        getStatus: function () {
           $http.get('catalog/statusCodes.json')
                .success(function (response) {
                    // 
                });
            }
        }
    };
}]);

Invoke function as -

statusCodesFactory.getStatus();

If you need to return response back to controller then use Promises. Inject $q service -

.factory('statusCodesFactory', ['$http', '$q', function ($http, $q) {
    return {
        getStatus: function () {
           var defer = $q.defer();
           $http.get('catalog/statusCodes.json')
                .success(function (response) {
                    defer.resolve(response);
                });
            }
           return defer.promise;
        }
    };
}]);

Then call factory method from controller as -

statusCodesFactory.getStatus().then(function(response){
    // Use response
});
0

The error is saying that you cannot call the method get on $http because $http is undefined. The $http parameter being passed to getStatus: function($http)... is the root of this problem. You need to figure out what is being passed to that function, and why is is an empty object.

MattCorr
  • 371
  • 3
  • 11