1

i have my authservice as given below ,

myApp.factory('Authentication',
  ['$rootScope', '$location', 'URL', '$http', '$q',
  function ($rootScope, $location, URL, $http, $q) {

  var myObject = {
    authwithpwd: function (user) {

      var dfd = $q.defer();
      $http
        .post('Mart2/users/login', {email: user.email, password: user.password})
        .then(function (res) {

          return dfd.resolve(res.data);


        }, function (err) {

          return dfd.reject(err.data);

        });

      return dfd.promise;

    } //login
  };
  return myObject;
}]); //factory

And i'm using that service in user service as follows :

myApp.factory('UserService',
  ['$rootScope', '$location', 'URL', '$http', '$q', 'Authentication',
  function ($rootScope, $location, URL, $http, $q, $Authentication) {

  var myObject = {
    login: function (user) {
      $Authentication.authwithpwd(user).then(function (regUser) {
        console.log(regUser);
      }).catch(function (error) {
        $rootScope.message = error.message;
      });
    },
    getUserToken: function () {
      return $rootScope.currentUser.apiKey;

    },
    isLogged: function () {
      if ($rootScope.currentUser) {
        return true;
      } else {
        return false;
      }
    }
    //login   
  };
  return myObject;
}]); //factory

I am very new to angular js . While writing service and calling that service from controller i have put a console debug in user service which is showing its returning object .i am getting object if i do console.log(regUser) ? any idea why ?

4
  • 2
    Can you clarify your question a bit? What is exactly your issue? Are you not getting the returned object in myObject? A fiddle or plunkr would be helpful as well Commented Apr 15, 2016 at 11:35
  • i am not getting returned promise from AuthenticationService in login method of userservice . i am getting [object,object] in line where i have done console.log(regUser) . i will try to create a fiddle for this too meanwhile Commented Apr 15, 2016 at 11:41
  • avoid the use of $rootscope -> stackoverflow.com/questions/36645399/… Commented Apr 15, 2016 at 11:42
  • When you are using promises, you should expect it to return an object in JSON format, when it resolves. Sometimes it can return an object even if rejected. You have to know what fields you are expecting to view them; if you are expecting an apiKey; console.log(data.apiKey); instead of console.log(data) which displays [object object] Commented Apr 15, 2016 at 11:45

1 Answer 1

1

To get the object you need to do change your myObject declaration. Basically you need to return a promise from the login function and then write a callback to get the resolved data.

myApp.factory('UserService',
  ['$rootScope', '$location', 'URL','$http','$q','Authentication',
  function($rootScope,$location, URL,$http,$q,$Authentication) {

  var myObject = {
    login: function(user) {
        var defer = $q.defer();
        $Authentication.authwithpwd(user).then(function(regUser) {
           console.log(regUser);
           defer.resolve(regUser);
      }).catch(function(error) {
           $rootScope.message = error.message;
           defer.reject(regUser);
      });
      return defer.promise;
   },
   getUserToken:function() {
      return $rootScope.currentUser.apiKey;
   },
   isLogged:function() {
      if($rootScope.currentUser){
           return true;
           } else {
             return false;
           }
      }//login   
   };
   return myObject;
}]); //factory

To extract the object from controller or from some other service you need to write a callback

UserService.login(user)
   .then(function (data) {
        $scope.data = data;
    }, function (error) {
        $scope.error = error;
    });

Also in the Authentication service you can just do a 'dfd.resolve' instead of 'return dfd.resolve'; since you are already returning the dfd.promise.
I have created a fiddler here

Sign up to request clarification or add additional context in comments.

2 Comments

You are combining factories and services, when in actuality they are 2 separate things in AngularJS.
how and when to use services and factory in this context , can you elaborate a bit in current context as done in above code .@EvanBechtol

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.