0

I want return object with some data, but when I try return 'doc' variable, this variable is empty object.

here is my code:

myApp.factory('serviceDocuments', function serviceDocuments($http) {
    var doc = {};
    return {
        getDocument: function (data) {
            $http({
                method: 'POST',
                url: '/getDocument',
                data: data
            }).then(function successCallback(response) {
                doc = response.data[0];
                // in this place doc variable has needed data
            }, function errorCallback(response) {
                console.log("ups... ;(");
            });
            // in this place doc variable is empty object
            return doc;
        }
    };
});
azernax
  • 15
  • 4

1 Answers1

-1

Your code is asynchronous. The line return doc is invoked before the callback for your http call (doc = response.data[0]).

john_omalley
  • 1,398
  • 6
  • 13
  • This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. - [From Review](/review/low-quality-posts/18776339) – 7urkm3n Feb 09 '18 at 18:38
  • What are you talking about @7urkm3n, He is right. OP's problem is the described above. – ecarrizo Feb 09 '18 at 22:31