2017-03-19 1 views
0

Il ya un an et demi, j'ai écrit une application angulaire qui utilisait cette authSrv pour se connecter et se déconnecter de l'utilisateur en utilisant des API dans mon backend et cela a parfaitement fonctionné. Je viens d'essayer d'exécuter le même code et je reçois un Error: [$injector:undef] Provider 'authSrv' must return a value from $get factory method.

Ceci est mon authSrv (usine) 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(); 
      }); 
     }); 
    }; 
}); 

Pourquoi est-ce que je reçois l'erreur maintenant et pas à l'époque? Quelque chose a changé?

Répondre

1

Vous devez user app.service(), et non app.factory(). Une fabrique est une fonction qui doit créer et renvoyer l'instance de service. Votre fonction est une fonction constructeur qui initialise this. C'est pour cela que service() est pour.

+0

Oui, ça l'a fait. et j'ai revérifié mon ancien code et c'était le service à l'époque. J'ai juste copié tout sauf 'app.service' et j'ai supposé que c'était une usine. J'ai tellement honte. Pouvons-nous supprimer cette question? :RÉ – Jhivan