AngularJS returns UNDEFINED to me in the RETURN

3

It happens that I have this function, which should return me a string in JSON , but it's not like that, it returns me undefined , why?

$scope.get = function(id, tabla){
          var data;
          if(id=="") {
            $http.get("api/" + tabla).then(function (response) {
              data = JSON.stringify(response.data.data);
              //Materialize.toast(response.data.statusMessage, 4000);



            }, function(response) {
            });
          } else {
            $http.get("api/" + tabla + "/" + id).then(function (response) {
                data = response.data.data[0];
                //Materialize.toast(response.data.statusMessage, 4000);
            }, function(response) {
            });
          }
          return data;
      }
    
asked by Anthony Medina 22.02.2017 в 16:15
source

3 answers

1

the problem is the asynchronity, you are not waiting for it to enter the conditionals, it directly returns the return, try with:

$scope.get = function(id, tabla){
          var data;
          if(id=="") {
            $http.get("api/" + tabla).then(function (response) {
              data = JSON.stringify(response.data.data);
              //Materialize.toast(response.data.statusMessage, 4000);
              return data;


            }, function(response) {
            });
          } else {
            $http.get("api/" + tabla + "/" + id).then(function (response) {
                data = response.data.data[0];
                return data;
                //Materialize.toast(response.data.statusMessage, 4000);
            }, function(response) {
            });
          }

      }
    
answered by 22.02.2017 в 16:31
0

When you make a method in angular that requires consuming a service, it makes a promise so it returns to you undefined or something strange like $ promise; I am telling you this so that you have it in mind for future methods A valid option is the one offered by x-rw , I will offer you a new form for educational purposes only

$scope.data = [];
$scope.get(1, 8); //ejemplo como si se estuviece consumiendo en el html
$scope.get = function(id, tabla){

          if(id=="") {
            $http.get("api/" + tabla).then(function (response) {
              $scope.data = JSON.stringify(response.data.data);
              //Materialize.toast(response.data.statusMessage, 4000);



            }, function(response) {
            });
          } else {
            $http.get("api/" + tabla + "/" + id).then(function (response) {
                $scope.data = response.data.data[0];
                //Materialize.toast(response.data.statusMessage, 4000);
            }, function(response) {
            });
          }
      }

and the variable data is available for any use within the scope of the variable scope of angular, when you work with methods that use promise (as a load of a json in a local way for example) you could believe that your method failed; on the other hand angular and its asynchrony leaves it as if it were running in the background, I recommend you when you use methods that are only actions that have to do with the variables that are working within the application that does not depend on a call to a service

    
answered by 22.02.2017 в 17:53
0

I already resolved this way, thanks for the support

$scope.get = function(id, tabla){
          var data;
          var defered = $q.defer();
          var promise = defered.promise;

          if(id=="") {
            $http.get("api/" + tabla).then(function (response) {
              defered.resolve(response.data.data);

              //Materialize.toast(response.data.statusMessage, 4000);



            }, function(response) {
               defered.reject(response);
            });
          } else {
            $http.get("api/" + tabla + "/" + id).then(function (response) {
                defered.resolve(response.data.data[0]);
                //Materialize.toast(response.data.statusMessage, 4000);
            }, function(response) {
               defered.reject(response);

            });
          }
          return promise;
      }

to call the function, I just call it in the following way.

        $scope.get('').then(function(data) {
            $scope.elementos = data;
        })

You always have to declare in the controller the header $q , otherwise, it will not work.

Anyway, I leave the tutorial by which I guide.

link

    
answered by 23.02.2017 в 19:00