0

I have the below function that has an http request in angular. In the success part, I am trying to return the provided data, and return the value in my setTextFile() function, but it is undefined. I have checked that data is not null or undefined, so I know it contains information, but I don't understand why it's not returning that content.

var showFile = function (txtFile, host) {

        var datData;
        var url = 'rest/archive-viewer/spanish-discount/' +
                  $rootScope.currentEnv + '/' +
                    host + '/' +
                    txtFile;

        $http.get(url).success(function (data) {

            $scope.sendingMessage = "Getting file";
            $scope.sending = false;
            $scope.sendingMessage = "";


            return data;


        }).error(function (data) {
            $scope.sendingMessage = "Creation failed";
            $scope.sending = false;

        });


    }

    $scope.pullFiles();


};

Here is my setTextFile function who's return value is undefined as a result of the showFile() return value.

 var setTextFile = function (file,host) {
        console.log("Is this null " + showFile(file, host));
        return showFile(file, host);
    }
Boaz
  • 19,892
  • 8
  • 62
  • 70
Rafa
  • 3,219
  • 4
  • 38
  • 70

1 Answers1

1

That http request is asynchronous. What I would suggest you to do is assigning data to a scope veriable something like : $scope.setTextFile = data in the success function you have passed in ajax request. Instead you can make a function to set the data and trigger the function inside success function.

binariedMe
  • 4,309
  • 1
  • 18
  • 34