0

Why second console.log gives me undefined when first one prints actual data? How to assign data received in promise to any variable to be able to use it without promise?

angular.module('dopasujApp')
    .controller('productLoader', [ '$scope','getProducts',
        function ($scope,getProducts) {
            $scope.recived = {};
            console.log($scope.list);
            function getter() {
                return getProducts(1, 150, 20, 'ASC', 'PRODUCT_ID', 'a').then(function (recived) {
                        $scope.recived = recived;
                        console.log($scope.recived);
                    }
                );
            }

            getter();
            console.log($scope.recived);
        }
]);

Here is my factory code:

angular.module('dopasujApp').factory('getProducts', ['$http', function ($http) {
 return function(fit = 1, limit = 150, vendor = 20, sort = 'ASC', orderBy = 'PRODUCT_ID', search='a', size=[], filters={}, attribs=[]) {
     var getProduct = {}, recived = {}, APIurl;
     getProduct.data={};
     getProduct.data.sort=sort;
     getProduct.data.orderBy=orderBy;
     getProduct.data.search=search;
     getProduct.data.filters=filters;
     getProduct.data.filters.ATTRIBS=attribs;
     getProduct.data.filters.SIZE=size;
     recived.list= [];
     recived.count = 0;
     recived.filters = {};
     APIurl = "*******?action=getListing&fit="+fit+"&limit="+limit+"&vendor="+vendor;
     var request = $http({
         method: "POST",
         url: APIurl,
         headers: {
            'Content-Type': 'application/json'
         },
         data: {
            data:  getProduct.data
         }
     });

     var values = request.then(function (response){
         return {
             list: response.data.QUERY,
             count: response.data.COUNT,
             filters: response.data.FILTERS
         };
     });
     return values;
 }
 }]);
2
  • Can you share your service code? Commented Sep 14, 2016 at 20:47
  • Sure, ill edit question. Commented Sep 15, 2016 at 8:27

1 Answer 1

3

Because your getProducts is a promise, and even tough your second console.log is after your getter();, the console.log is executed before your promise is resolved, thats why its undefined.

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

5 Comments

to add to this... getter().then(function() { console.log($scope.recived);}) would be proper way to do second log
Ok, but how to get actual data? I mean, how to assign actual received data to $scope.recived or any other variable to able to use it without promise?
You are already assigning it, arent you? With $scope.recived = recived; inside the promise?
Yeah the promise waits for the data, and the console.log runs immediately. You need to wait for the getter... ie @charlietfl getter().then() is the way to go.
So it prints undefined not because I did it wrong, but because it not done yet when im trying to print it? Eh, now i get it... So actually using promise in factory gives me nothing, right?

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.