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 ?

Rajeshwar
  • 2,290
  • 4
  • 31
  • 41
Sanjay
  • 117
  • 4
  • 13
  • 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 – Pratik Bhattacharya Apr 15 '16 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 – Sanjay Apr 15 '16 at 11:41
  • avoid the use of $rootscope -> http://stackoverflow.com/questions/36645399/save-current-user-information-and-use-it-in-controllers/36645460?noredirect=1#comment60884162_36645460 – thegio Apr 15 '16 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]` – Evan Bechtol Apr 15 '16 at 11:45

1 Answers1

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

Pratik Bhattacharya
  • 3,596
  • 2
  • 32
  • 60
  • You are combining factories and services, when in actuality they are 2 separate things in AngularJS. – Evan Bechtol Apr 15 '16 at 11:46
  • 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 – Sanjay Apr 15 '16 at 12:01