Search code examples
angularjsangular-factory

Provider 'kk' must return a value from $get factory method


A year and a half ago i wrote an angular app which used this authSrv to login and logout user using APIs in my back end and it worked perfectly. I just tried to run the same code again and i get a Error: [$injector:undef] Provider 'authSrv' must return a value from $get factory method.

This is my authSrv(factory) code:

app.factory('authSrv', function ($rootScope, $http, $auth, $state, config, $q) {

    var deferred = $q.defer();

    this.login = function (email, password) {
        // Show loading dialog
        $rootScope.showLoading();

        var credentials = {
            email: email,
            password: password
        };

        return $auth.login(credentials)
            .then(
                function (result) {
                    $http.get(config.server + 'restricted/user')
                        .then(function (response) {
                            // Stringify the returned data to prepare it
                            // to go into local storage
                            var user = JSON.stringify(response.data.user);

                            // Set the stringified user data into local storage
                            localStorage.setItem('user', user);

                            // The user's authenticated state gets flipped to
                            // true so we can now show parts of the UI that rely
                            // on the user being logged in
                            $rootScope.auth = true;

                            // Putting the user's data on $rootScope allows
                            // us to access it anywhere across the app
                            $rootScope.currentUser = response.data.user;

                            // Remove loading dialog
                            $rootScope.hideLoading();

                            // Everything worked out so we can now redirect to
                            // the users state to view the data
                            $state.go('app.artists');

                            deferred.resolve(response.data.user);
                            // promise is returned
                            return deferred.promise;
                        });
                },
                function (error) {
                    // the following line rejects the promise
                    deferred.reject(error);

                    // Remove loading dialog
                    $rootScope.hideLoading();

                    // promise is returned
                    return deferred.promise;
                });
    };

    this.logout = function () {
        // Show loading dialog
        $rootScope.showLoading();

        $auth.logout().then(function () {
            // Remove the authenticated user from local storage
            localStorage.removeItem('user');

            // Flip authenticated to false so that we no longer
            // show UI elements dependant on the user being logged in
            $rootScope.auth = false;

            // Remove the current user info from rootscope
            $rootScope.currentUser = null;

            $state.go('app.auth').then(function () {
                // Remove loading dialog
                $rootScope.hideLoading();
            });
        });
    };
});

Why do i get the error now and not back then? Has anything changed?


Solution

  • You should user app.service(), not app.factory(). A factory is a function that should create and return the service instance. Your function is a constructor function, that initializes this. That's what service() is for.