0

I am a beginner and working on existing code, making few changes to post payload.

I am making a get call inside a factory like this,

var providerUrl = "/" + OPENMRS_CONTEXT_PATH + "/ws/rest/v1/session";
\$http.get(providerUrl).then(function(response){
var uuid =response.data.user.uuid;
console.log(uuid);  //1st console.log
}, function(error){
console.log(error);
});
console.log(uuid); //2nd console.log

Now the 2nd log doesn't actually shows anything probably because of the asynchronous nature.

I need to use this UUID value in the factory itself. Because I am posting a payload using $http post call like this from the same factory,

  var json = {
          patient: patient,
          uuid: ....   //HERE GOES THE GET RESPONSE
          encounterType: window.constantConfigObj.encounterTypeVisitNote,
          visit: visitId,
          encounterDatetime: date2
  };

If someone can explain me how this can be done, it would be really helpful. Thanks

1 Answers1

0
   function getdata(){
 var providerUrl = "/" + OPENMRS_CONTEXT_PATH + "/ws/rest/v1/session";

    return $http.get(providerUrl).then(function(response){
        return response.data.user.uuid;
    }, function(error){
        console.log(error);
    });
}



var json = {
          patient: patient,
          uuid: getdata,
          encounterType: window.constantConfigObj.encounterTypeVisitNote,
          visit: visitId,
          encounterDatetime: date2
  };

in controller

factory.uuid().then((resp) => {console.log(resp)})
zabusa
  • 2,520
  • 21
  • 25
  • the function getdata() is returning a $$state array which consists 'status' and 'value' keys with their values, I only require value of 'value' key. Attaching the screenshot, check the payload - [link](https://drive.google.com/open?id=17U_vI_h-tyWFXS3i-QkZZ4vjN7zhRO8y) – Hardik Nagda Dec 19 '17 at 10:36
  • @HardikNagda then only return the value key the $http return.and you need uuid? or $$state object – zabusa Dec 19 '17 at 10:38
  • I only want to return the 'value' key, not the entire $$state object i.e in this case it is a4ac4fee- .... something (according to the screenshot) – Hardik Nagda Dec 19 '17 at 10:44
  • how you getting the entire state? you just want the $http response right?made some changes try those – zabusa Dec 19 '17 at 10:46
  • Console.logging return value from function gives me $$state object which has attributes - status : 1 and value : "THE VALUE I WANT" , How do i retrieve the value i want – Hardik Nagda Dec 19 '17 at 10:48
  • i dont understand why you telling me $$state? you need the http response right? then why always $$state – zabusa Dec 19 '17 at 10:50
  • Nah the changes you made didnt work... Still getting same return i.e. $$state – Hardik Nagda Dec 19 '17 at 10:50
  • when you cansole.log(resp )what will you get? – zabusa Dec 19 '17 at 10:52
  • [link](https://drive.google.com/open?id=1_kO4ljwhjWPO-uiUnvxice_wuyAy31dE) This is what i get on console.log – Hardik Nagda Dec 19 '17 at 10:54
  • ithink you are not doing then .then function only calling uuid().do this `resp.then(function(result){console.log(result.value)})` – zabusa Dec 19 '17 at 10:56
  • But what if i want to save the resolved promise into scope, how do i do that? – Hardik Nagda Dec 24 '17 at 06:14