Take value of variable in function return

Asked

Viewed 798 times

1

$scope.fetchData = function (param) {
   var url = param;
   var thumb;
   var imgid = get_numbers(url);
   var hash = ("http://vimeo.com/api/v2/video/" + imgid + ".json");
   $http({
     method: 'GET',
     url: hash
   }).then(function successCallback(response) {
     thumb = response.data[0].thumbnail_medium;
   }, function errorCallback(response) {
      console.log("Request fail");
   });
   return thumb;
};


function get_numbers(input) {
   return input.match(/[0-9]+/g);
}

var imgthumb = $scope.fetchData("http://player.vimeo.com/video/219839519");
alert(imgthumb);

Example: when I do console.log of the Thumb variable inside the function then, I can see the value of the variable, but when I return the value of the variable and undefined.

  • Colleague, describe your problem better and format your code.

  • Catch what? Where?

2 answers

1

gets like this

 $scope.fetchData = function (param) {
                             var defer = $q.defer();
                            var url = param;
                            var thumb;
                            var imgid = get_numbers(url);
                            var hash = ("http://vimeo.com/api/v2/video/" + imgid + ".json");
                            $http({
                                method: 'GET',
                                url: hash

                            }).then(function successCallback(response) {

                                thumb = response.data[0].thumbnail_medium;
                                return  defer.resolve(thumb);

                            }, function errorCallback(response) {
                                return deferred.reject('fail');
                            });

                            return defer.promise;
                        };


                        function get_numbers(input) {
                            return input.match(/[0-9]+/g);
                        }

                        var imgthumb = $scope.fetchData("http://player.vimeo.com/video/219839519");

                        console.log(imgthumb);

0

From what I understand, you are having difficulty receiving the return in the variable imgthumb, because http.get runs asynchronously. For that, do:

$scope.fetchData = function (param) {
   var url = param;
   var thumb;
   var imgid = get_numbers(url);
   var hash = ("http://vimeo.com/api/v2/video/" + imgid + ".json");
   return $http({
     method: 'GET',
     url: hash
   });
};


function get_numbers(input) {
   return input.match(/[0-9]+/g);
}

$scope.fetchData("http://player.vimeo.com/video/219839519").then(function successCallback(response) {
      alert(response.data[0].thumbnail_medium);
   }, function errorCallback(response) {
      console.log("Request fail");
   });
  • ok, so that’s all right, but when it’s time to return the function the value of the variable comes as Undefined.

  • Which variable is Undefined??

  • ai, Thumb = Sponse.data[0]. thumbnail_medium; When doing a Return in the main function is Undefined

  • No use wanting to return from an asynchronous function, @Humbertolopes.

  • ok, solved, something was missing from the Angularjs, the var Defer = $q. Defer();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.