Forum Discussion

DeanOusterhout's avatar
DeanOusterhout
Qrew Assistant Captain
8 years ago

javascript variable becomes ""not defined"" after next promise call

I am executing a query through promise, and am able to retrieve a field into a javascript variable.

Unfortunately, after the next promise call, that variable is now showing undefined.

How to I get the contents of that first variable to be "global" so I can use it farther down in the nested set of promises?

Thanks,
Dean

10 Replies

  • DeanOusterhout's avatar
    DeanOusterhout
    Qrew Assistant Captain
               var promise1 = $.get(WorkflowTaskOrderID, {                 act: "API_DoQuery",
                     query: "{'17'.EX." + WorkflowTask + "}AND{'22'.EX." + Status + "}",
                     clist: "17.24.9.20.22.23.24.29.30.31.32.33.37.44"
                 }); // end promise1

                 $.when(promise1).then(function(xml1a) {
                     console.dirxml(xml1a);
                     var ContinueNextTaskNow = $("continue_next_task_now", xml1a).text();
                         var qbu_url_1a = "";
                         qbu_url_1a += qbu_domain;
                         qbu_url_1a += LeadAssignmentTaskTableId;
                         qbu_url_1a += "?act=API_DoQuery";
                         qbu_url_1a += "&query=" + qbu_criteria_1a;
                         qbu_url_1a += "&clist=44.5.3";
                         qbu_url_1a += "&apptoken=" + qbu_apptoken;

                         // alert("qbu_url_5 ("+qbu_url_5+ ")" );

                         var promise1a = $.get(qbu_url_1a);

                         $.when(promise1a).then(function(xml) {
                             console.dirxml(xml);

                            //At this point the value of ContinueNextTaskNow is undefined...
  • DeanOusterhout's avatar
    DeanOusterhout
    Qrew Assistant Captain
    what is strange about this is that I have a whole lot of variables that stay active throughout the entire script... it s just this one that is causing the issue. 
  • DeanOusterhout's avatar
    DeanOusterhout
    Qrew Assistant Captain
    Hello Dan,

    It is now working.  In fiddling around with it, I made this...

    var ContinueNextTaskNow = $("continue_next_task_now", xml1a).text();               
    var testContinueNextTaskNow = ContinueNextTaskNow;

    into this:
    var ContinueNextTaskNow = $("continue_next_task_now", xml1a).text();                
    var testContinueNextTaskNow = $("continue_next_task_now", xml1a).text();

    Now, the first variable... ContinueNextTaskNow is holding it's value, but the last variable (testContinueNextTaskNow ) is not.

    Any thoughts as to why that might be happening?

    Thanks for your effort on this...

    Dean
    • _anomDiebolt_'s avatar
      _anomDiebolt_
      Qrew Elite
      It is undoubtedly a scope problem because of the way your code is constructed and its length.

      I noticed you have all of these comments in the code reminding you of where a block ends:
              } // end of if found next task...  
      This is a code smell that you are indenting too much to hold the function of the code in your mind all at one time. You might get it working by wrapping things in try catch blocks, creating more global variables and throwing a ton of debug commands around but this approach is unsustainable. You should retrench and adopt the idioms I have mentioned. I will comment further when I get some more time.
  • I think the essential problem you are having is associated with this statement:

     "so I can use it farther down in the nested set of promises?"

    Promises should not normally nest - they should chain instead:

    $.get(...)
      .then(function(xml) {
        return $get(...)
      }).then(function(xml) {
        return $get(...)
      }) .then(function(xml) {
        return $get(...)
      })

    However there is a problem with the version of jQuery QuickBase uses (1.7.2) where you have to use the pipe() method instead of then() to properly pass responses to the next then() in the chain.

    The only reason you should see nesting (even in some of my earlier code) is to avoid using jQuery's pipe() method as it is deprecated in more recent versions.

    It is a weird scenario caused by QuickBase not updated their version of jQuery beyond 1.7.2

    Also you are using a lot of old constructs - even if you drew them from some of my older code.

    For example 

    var promise0 = $.get(LeadAssignmentTaskTableId, {
      act: "API_DoQuery",
      query: "{\'3\'.EX." + AssignedTask + "}",
      clist: "7.8.9.12.56.77.44.5"
    });
    $.when(promise0).then(function(xml) {
      console.dirxml(xml);
    });

    Should probably be replaced by this idiom which removes the promise0 variable and chains directly into the next level of the pipeline:
     
    $.get(LeadAssignmentTaskTableId, {
      act: "API_DoQuery",
      query: "{\'3\'.EX." + AssignedTask + "}",
      clist: "7.8.9.12.56.77.44.5"
    }).then(function(xml) {
      console.dirxml(xml);
    });

    I have a limited amount of time now but I will reply further as there are a lot of other things in your code that are just line noise which makes it difficult to debug.
  • DeanOusterhout's avatar
    DeanOusterhout
    Qrew Assistant Captain
    Thanks Dan,

    I am not a professional coder, although have been in various IT roles for quite a while.  I am interested in doing this the correct way and learning.  I have learned a lot from your methods, and appreciate all you give to the community.

    thanks,
    Dean 
  • DeanOusterhout's avatar
    DeanOusterhout
    Qrew Assistant Captain
    Thanks Dan for the suggestion on chaining versus nesting.  That approach is certainly much easier to follow and easier to read.  

    I tried that with another script, and I think i did not get it quite right.  The second $get is not returning the new values into the function(xml).  

    If you have a chance to look at it I would greatly appreciate it.  It is under a different question, 

    Chained Jquery Call Not Working

    thanks, Dean