1

OK so Im trying to create a Factory that will make a call to my REST api to retrieve data. All was fine until I updated to Angular 1.6 which forced me to use Promises and so I decided to try factories out:

var registerApplication = angular.module('applications', ['ajoslin.promise-tracker']);

registerApplication.factory('HTTPFactory', ['$http', function dataService($http) {

    var applications = {};

        applications = function getApplications() { $http ({
                'url': '/adminapi/getApplications',
                'method': 'GET',
                'cache' : true
            }).then(function(response){
               // return response.data;
                console.log(response);
                return response;
            });
        }
    return applications;
}]);

registerApplication.controller("ApplicationController", ['HTTPFactory',     function ($scope, HTTPFactory) {

    $scope.applications = {};
    getApplications();

    // when landing on the page, get all applications and show them
    function getApplications() {
        HTTPFactory.getApplications().then(function(response){
            $scope.applications = response;
        });
    }
}]);

However I'm getting this error: angular.js:10859Error: undefined is not an object (evaluating 'HTTPFactory.getApplications')

I've tried several ways and all seem to give me an error of some form, what am I doing wrong???

2 Answers 2

1

Factory is not properly injected.

registerApplication.controller("ApplicationController", ['HTTPFactory', function ($scope, HTTPFactory) {

should be

registerApplication.controller("ApplicationController", ['$scope', 'HTTPFactory', function ($scope, HTTPFactory) {

Furthermore, the following piece of code needs to be fixed.

applications = function getApplications() {
    // Logic
}

Should be replaced with by fixing the closure problem and also removing .then() from the logic of the factory.

applications.getApplications = function() {
    return $http({
        'url': '/adminapi/getApplications',
        'method': 'GET',
        'cache': true
    });
}
Sign up to request clarification or add additional context in comments.

5 Comments

Fixed that problem but now I get Error: HTTPFactory.getApplications is not a function. (In 'HTTPFactory.getApplications()', 'HTTPFactory.getApplications' is undefined) which was a error I was previously having prior to making some changes.
@RudolphRedNose please review.
Thank you it worked! Now to figure out why my UI didn't update... One step closer though. Thanks again.
You are welcome. Please upvote and select the answer if you may pls.
HTTPFactory.getApplications().then(function(response){ $scope.applications = response; }); Please log the console and check if you are getting the correct response.
1

first change the factory like this

registerApplication.factory('HTTPFactory', ['$http', function ($http) {

    var applications = {
          getApplications : getApplications
    };

        function getApplications() { 
            return $http ({
                'url': '/adminapi/getApplications',
                'method': 'GET',
                'cache' : true
            })
        }
    return applications;
}])

you don't have to write the promise inside factory just return the http req to controller and catch the promise in there.

In the controller add string scope to array

registerApplication.controller("ApplicationController", ['$scope','HTTPFactory', function ($scope, HTTPFactory) {

registerApplication.controller("ApplicationController", ['$scope','HTTPFactory',     function ($scope, HTTPFactory) {

    $scope.applications = {};
    getApplications();

    // when landing on the page, get all applications and show them
    function getApplications() {
        HTTPFactory.getApplications().then(function(response){
            $scope.applications = response;
        });
    }
}])

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.