Fixing AngularJS error: Provider must return value from $get factory method

后端 未结 2 1265
日久生厌
日久生厌 2021-01-26 01:58

When I use the code below, I am getting the following error:

Provider \'Login\' must return a value from $get factory method.

I\'ve

相关标签:
2条回答
  • 2021-01-26 02:02

    You must return an object or function or at leat a value from the factory

    0 讨论(0)
  • 2021-01-26 02:09

    In terms of factory you must return an object. Since you're not returning anything, it means that other services/controllers can't use this service.

    If you're just checking for Authentication, it must be inside your Auth service which must be an IIFE function. Which will check and redirect the user.

    For example:

    In either Auth/Ref service, create a IIFE

    (function() {
    var authData = Ref.getAuth();
    
       if (authData) {console.log('already logged in with ' + authData.uid)} else {
    
          return Auth.$authAnonymously({rememberMe: true}).then(redirect, showError);
    
          function redirect() {
          $location.path('/account');
        }
    
          function showError(err) {
          Login.err = err;
        }
      }
    })();
    

    Else insert the code inside init() method and call that in your service. SO this will run only once.

    You must use service when you want to expose a singleton interface for other parts of your application to make use of.

    0 讨论(0)
提交回复
热议问题