0

I have a controller like below:

.controller('googleFormsCtrl', function ($scope, $http, $window) {
    // SEND THE HTTP REQUEST
    var url;
    var allForms;
    var externalWindow;
    // MAKE API REQUEST for Authorization
    $http.get('/api/googleAuth').then(function (response) {
        url = response.data;
    });

    this.googleAuthentication = function () {
        externalWindow = $window.open(url, "Please Sign in with Google", "width:350px,height:350px");
    };

    // Close Authorization and set credentials
    window.onmessage = function (info) {
        externalWindow.close();
        // Get the CODE from the URL
        var urlCode = info.data;
        // Get pure code
        var idx = urlCode.lastIndexOf("code=");
        var code = urlCode.substring(idx + 5).replace("#","");
        // Get TOKEN
        $http.get('/api/googleToken?code=' + code).then(function (response) {

            console.log(response.data);
            allForms = response.data;

        });

    };

    console.log("HERE IS UNDEFİNED " + allForms);

    // Fill Drop Down with Forms
    this.fillForms = function () {

    }
})

I have a window.onmessage function and within it I have an http GET requet which retrieves data to me. I am trying to retrieve it by assigning its value to "allForms" variable so that ı can use it in another function within controller, however it retrieves undefined value.How can I retrieve it? Thanks in advance.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
Habil96
  • 75
  • 1
  • 10
  • 2
    Possible duplicate of [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – SLaks Sep 17 '18 at 13:40
  • Hi. First, welcome to stackoverflow. Second, the fundamental issue is your execution order...think about this for a moment: you set up a message handler and inside that message handler, you send out an http request. Inside the handler of its response, you assign the data being returned to a variable. But back up at the top, where you registered your message handler, you expect the `allForms` variable to hold a value immediately. It might not be what you are looking for, but if you move your console.log into the callback from the $http.get call, you will log the value if one is available. – K. Alan Bates Sep 17 '18 at 13:54
  • Thank you very much @K.AlanBates, could you please provide more detail. I need it in global scope within controller. Where should ı move console log? – Habil96 Sep 17 '18 at 14:03
  • @Habil96 you're going to run into serious issues trying to do that(treat your async responses as if they are sync). The timing of the assignment to the `allForms` variable is non-deterministic. There is no guarantee that that assignment will ever happen. What you *can* do is, from your http callback, send a message out to another receiver and push the `allForms` variable through the message or notify them that it is ready for pickup. – K. Alan Bates Sep 17 '18 at 14:24
  • So to back up a bit: why is it that you are needing your `allForms` variable available to the controller "globally?" – K. Alan Bates Sep 17 '18 at 14:41
  • @K.AlanBates because ı need to access user's all forms, in order to access them by their is later – Habil96 Sep 17 '18 at 17:39
  • @Habil96 I think that where you're starting from, you might have more success by not trying to access the variable directly through the controller. Initialize your variable when the controller is created, assign to it through the callback the way you are, but then junk the code that is trying to treat it as if the value came in procedurally. Define your own message semantics (easy way is to add a "topic" property to the message) and window.postMessage it out. Have your existing controller receive the message, interpret, and then post another Message out with the `allForms`. – K. Alan Bates Sep 17 '18 at 20:38
  • Then have your consumer that depends on `allForms` receive it through its own registered message handler and process it only within this handler. To rephrase: have the controller hold the `allForms`, have your consumer send a message asking for `allForms`, have your controller send a message out that contains `allForms` then have your relying consumer receive the message and process the receipt of `allForms`. It's a touch more intricate than a naive assignment call, but will get you much more mileage once you can get it set up well. – K. Alan Bates Sep 17 '18 at 20:41

0 Answers0