0

I have this function on my controller:

   // Get a specific Parte
    partesc.getParte = function (id) {
            var url = endpointApiURL.url + "/fabricante/" + id;
            $scope.PartesPromise = $http.get(url)
                .then(function (response) {
                    partesc.parte= response.data;
                })
                .catch(function (error) {
                    console.log(error);
                    if (error.status == '412') {
                        console.log('Error obteniendo datos: ' + error.data.error);
                    }
                });
        }

And i have this second function:

partesc.openEdit = function(id) {
partesc.getParte(id);
    console.log(partesc.parte); }

I call openEdit function from a button on the front end. So the console.log part prints undefined. I think that is not waiting for the response of the calling the function getParte(id).

How i can make that wait for the response of the function to print the result? i'm doing this on the wrong way?

UPDATE 1

The console.log is just for reference. I need use the data that return the another function (getParte) inside the another one (openEdit)

SOLUTION

I find the solution thanks to the answer that i accepted here.

        // Get a specific Parte
    partesc.getParte = function (id) {
            var url = endpointApiURL.url + "/fabricante/" + id;
             return $http.get(url)
                .then(function (response) {
                    partesc.parte= response.data;
                })
                .catch(function (error) {
                    console.log(error);
                    if (error.status == '412') {
                        console.log('Error obteniendo datos: ' + error.data.error);
                    }
                });
        }


partesc.openEdit = function(id) {
    $scope.PartesPromise = partesc.getParte(id)
                            .then(function() {
                                console.log(partesc.parte);
                                }); 
}

Thanks

5
  • can you log from here ? $http.get(url) .then(function (response) { partesc.parte= response.data; console.log(partesc.parte); }) Commented Feb 2, 2017 at 15:56
  • 1
    you can return the promise from the function itself and then use .then(successCallback) on the returned promise object. Commented Feb 2, 2017 at 15:56
  • @Groben yes... this brings a object with data. Commented Feb 2, 2017 at 15:57
  • what do you want to do ? Commented Feb 2, 2017 at 15:58
  • I update my answer see the update 1 my friend @Groben thanks Commented Feb 2, 2017 at 16:01

5 Answers 5

1

Then if you can return the promise:

 partesc.getParte = function (id) {
     var url = endpointApiURL.url + "/fabricante/" + id;
     return $http.get(url);
 };

partesc.openEdit = function(id) {
    partesc.getParte(id).then(function(response){
    // stuff you want to do
    });
};
Sign up to request clarification or add additional context in comments.

2 Comments

This works my friend. Thanks a lot! I update my answer thanks to you.
Careful, I think the way you updated your answer doesn't allow your code to work.
1

You can use the promise to do so:

partesc.getParte = function (id) {
        var url = endpointApiURL.url + "/fabricante/" + id;
        return $scope.PartesPromise = $http.get(url)
            .then(function (response) {
                partesc.parte= response.data;
            })
            .catch(function (error) {
                console.log(error);
                if (error.status == '412') {
                    console.log('Error obteniendo datos: ' + error.data.error);
                }
            });
    }

This will return the promise, so you can wait for the resolve or reject in your controller, like so:

partesc.openEdit = function(id) {
    partesc.getParte(id).then(function() {
        console.log(partesc.parte);
    }); 
}

1 Comment

I try this my friend and i have an error: "TypeError: Cannot read property 'then' of undefined"
0

Use console.log inside then function.

partesc.getParte = function (id) {
            var url = endpointApiURL.url + "/fabricante/" + id;
            $scope.PartesPromise = $http.get(url)
                .then(function (response) {
                    partesc.parte= response.data;
                    console.log(partesc.parte);
                })
                .catch(function (error) {
                    console.log(error);
                    if (error.status == '412') {
                        console.log('Error obteniendo datos: ' + error.data.error);
                    }
                });
        }

Because this is async call and next line of code gets executed after your function call and till then there is no response so you get value as undefined outside your function.

2 Comments

Thanks for the answer... The console.log is just for reference. I need use the data that return the another function (getParte) inside the another one (openEdit)
You can call openEdit with the data returned in place of console.log
0

You can use the $q service in order to overcome the problem

// inject $q in the controller

partesc.getParte = function (id) {
           var deferred = $q.defer(),
              url = endpointApiURL.url + "/fabricante/" + id;
            $scope.PartesPromise = $http.get(url)
                .then(function (response) {
                    partesc.parte= response.data;
                    deferred.resolve(response.data);
                })
                .catch(function (error) {
                    console.log(error);

                    if (error.status == '412') {
                        console.log('Error obteniendo datos: ' + error.data.error);
                    }

                 deferred.reject(error);
                });

           return deferred.promise;
        }

And the use the above function as follows:

partesc.openEdit = function(id) {
partesc.getParte(id).then(function(response){
       partesc.parte = response
       console.log(partesc.parte);
      })
      .catch(function(error){
      }); 
}

Comments

0

You can return promise and from the function use it as below:

angular.module("myApp",[])
.controller('ctr1', function(sample){
 sample.sampleFn().then(function(data){
  console.log(data);
 })
})
.factory('sample', function($http){
  return {
    sampleFn : sampleFn
  }

 function sampleFn(){
    return $http.get('response.json').then(function(response){
            return response.data;
         }, function(){
            $q.reject("Failed");
        })
 }
})

Sample Working Plunk

1 Comment

Grt. Happy to help :-)

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.