0

I want to call v = "'"+u+"'" value into post method and i declared them as global variable. I'm not able to console those values outside get method. It's throwing me error. How to fix this ?

scope.Ex = function() {
            var u,w,v,c,is;

            $http.get("/api/patients/")
            .then(function(response) {
                scope.content = response.data;
                c = scope.content;
                is =c["0"].patient_ID;
                u = '/api/patients/'+is+'/';
                v = "'"+u+"'"

            });

            console.log(v);
            $http.post("/api/exams/",
                        {
                            patient_ID : v // I want to call V here from get method
                            // patient_ID : "/api/patients/1/"
                        })
                        .error(function(err){

                            //console.log(err);
                        })
                        .success(function(response) 
                        {
                            //console.log(v);
                            //console.log("Success")
                            //console.log(response);
                            //$scope.usersData = response;
                        });
        };
Sai Manoj
  • 3,139
  • 1
  • 8
  • 30
  • Chain the two promises. For more information, see [AngularJS $q Service API Reference - Chaining promises](https://docs.angularjs.org/api/ng/service/$q#chaining-promises). – georgeawg Apr 02 '18 at 15:44
  • Also the [`.success` and `.error` methods have been deprecated and removed from v1.6](https://stackoverflow.com/questions/35329384/why-are-angular-http-success-error-methods-deprecated-removed-from-v1-6/35331339#35331339). – georgeawg Apr 02 '18 at 15:45

1 Answers1

0

Chain the GET and POST operations:

$http.get(url)
  .then(function(response) {
    return response.data;
}).then(function(data) {
    return $http.post(url,data);
})

It is imortant to return data and promises to the handler functions.

For more information, see AngularJS $q Service API Reference - Chaining promises.

georgeawg
  • 46,994
  • 13
  • 63
  • 85