0

I have a following code where controller use factory function to get user data via an api request.

controller

myApp.controller('UserApiCtrl', function($scope,$auth,Account){

    $scope.getProfile = function(){
        Account.getProfile()
            .then(function(response){

                $scope.user = response.data;

            })

            .catch(function(response){
                // errors handled here
            })
    };

    $scope.getProfile();

})

factory

angular.module('MyApp')
    .factory('Account', function($http){
        return {
            getProfile: function(){
                return $http.get('/api/me');
            }
        }
    });

Now in controller when I console.log(response.data), json data is available, but when I console.log($scope.getProfile()) it's undefined.

Thanks!!

5
  • 2
    Because your function is not returning any value. You should return the values/object on success callback. Commented Sep 23, 2015 at 12:51
  • @Vineet return $scope.user doesn't work.. Commented Sep 23, 2015 at 12:53
  • 1
    the call Account.getProfile() is async, and your call to the $scope.getProfile() will not wait till the call back is executed. Even if you return something in the callbak, there is no one to catch it. Commented Sep 23, 2015 at 13:01
  • 1
    This looks to be normal, expected behavior with async functions. The entire point of an async function is to allow your code to continue to do other work while waiting for something (in this case, a response from the server). Anything in the .then block is going to happen in the future, anything outside happens now, You can't log now something that you expect to happen in the future. Commented Sep 23, 2015 at 13:12
  • in the case of angular, because angular creates an event watcher on $scope, when the future event finishes ($scope.user is populated with a value), then angular will run a $digest and update the DOM. Commented Sep 23, 2015 at 13:16

2 Answers 2

1

Here is the answer, read the below point to understand the concept.

  1. You are assigning value into scope variable $scope.user , So you can do logging for it.
  2. You are getting response back in success, so again you can log here also.

But when you try to log console.log($scope.getProfile()) , it is not going to return anything. In angular every thing you can keep in scope object.

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

1 Comment

This make sense.. Thanks
0

If you get response.data then below code should work fine.

myApp.controller('UserApiCtrl', function($scope,$auth,Account){
    $scope.user = {}; // Change to [] if response.data is array
    $scope.getProfile = function(){
        Account.getProfile()
        .then(function(response){
            $scope.user = response.data;
            console.log($scope.user);
        })
        .catch(function(response){
                // errors handled here
            })
    };
    $scope.getProfile();
});

4 Comments

It return me an empty Object{}.
@novavent, try logging the response.data also, if you are getting valid value, then for sure you should get data for $scope.user also.
Inside the function I can get the json data, but only outside when I log the getProfile() I get nothing but undefined/Object {}. Anyway my view still able to use data from the $scope.
You will not get. As suggested by @Thangadurai. getProfile method is Async. You can use it only after getting success or failure response.

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.