Forum Discussion

JohnBarulich1's avatar
JohnBarulich1
Qrew Cadet
7 years ago

JavaScript API_DoQuery failing to give the proper xml response

function getAndSetList() {
var url = "blahblah";
getPerm();
$.get(url, {
act: "API_DoQuery",
query: "{66.IR.'last wk'}",
clist: "66"
}).then(function(xml){
console.dirxml(xml);

});
}

The above code processes the xml, but it does not find any records. FID 66 is an [End Mileage] field and I would like to see all the records with this field for the last week. I don't think there is a syntax issue with the query parameter but there is definitely something wrong with it, because I can query other fields without using IR (Is During).
  • There is a very simple explanation and fix but let me answer first by proposing a metaphor. Your situation is very similar to ordering a meal at a restaurant and immediately complaining that you didn't received anything because you didn't wait for the order to be placed, cooked and served.

    We normally think of a computer as executing one instruction after another with each instruction completing before the next instruction is started. When making network requests we have to modify this step by step model because when a network request is made it takes an unpredictable amount of time before the response is actually received.

    When you make a network request using jQuery's $.get(), $.post(), $.ajax(), or $.getScript() methods what is returned is not the response but an object called a promise. A promise is like a claim check at the laundry or a number ticket at the bakery in that the promise is the mechanism by which you will eventually get your result when it become available.

    One way to patch up your code is to make the function getAndSetList() return a promise. Here is a version of your code that makes url a parameter to getAndSetList() and more importantly returns a promise:

    function getAndSetList(url) {
      getPerm();
      return $.get(url, {
        act: "API_DoQuery",
        query: "{66.IR.'last wk'}",
        clist: "66"
      });
    }

    var url = "blahblah";
    getAndSetList(url).then(functioin(xml) {
      console.dirxml(xml);
      //your further code here
    });

    Note that I have no idea what getPerm() does as it does not appear to influence the function and you did not provide any further details.
  • Thanks for the reply as usual Dan,

    One thing I'm curious about is the conceptual difference between your code and mine. My $.get() call does return a promise. Your code also returns a promise. Both code snippets, yours and mine, wait for the request to finish before proceeding. What's the subtlety here that I am missing?

    Edit: Also, no need to worry about getPerm(), it doesn't influence this code, I should've just left it out.

    Edit: From further analysis, it seems that your code is conceptually equivalent. I get no error from the API call, but no records are processed into the xml. I ensured the link to the table is correct. It works successfully if I query the same field but with different constraints (i.e. {'66'.EX.'0'} , also no query parameter). getPerm() returns $.ajaxSetup call. I turned async off for debugging but still, no response. I think it has to deal with QuickBase's back end implementation of the "{66.IR.'last wk'}".

    Thanks
    • _anomDiebolt_'s avatar
      _anomDiebolt_
      Qrew Elite
      >My $.get() call does return a promise.

      That is true but the function getAndSetList() does not return a promise (it can't because there is no return statement).
    • JohnBarulich1's avatar
      JohnBarulich1
      Qrew Cadet
      getAndSetList() does not return a promise because the promise happens within the function after the $.get() call. Your code snippet also conceptually waits to process the xml as well, just outside of the function. Both your code, or mine, do not work for some reason. 

      Why?
    • _anomDiebolt_'s avatar
      _anomDiebolt_
      Qrew Elite
      Unless getAndSetList() returns a promise it will run to completion and return undefined before the promise within it has a chance to resolve. Once getAndSetList() returns it looses scope and the promise within it is lost.
  • UrsulaLl's avatar
    UrsulaLl
    Qrew Assistant Captain
    What type of field is  [End Mileage] ? is it a text field, or numeric, or date? If it is not a text field, then it will never return anything, since you are searching for text: 'last wk'.
    If it is a text field, then maybe last week, there were no records. I would test this by putting in other text, like 'last month' or 'two weeks' or any other responses this field may have. It may be possible that it isn't returning anything due to that. 
    • JohnBarulich1's avatar
      JohnBarulich1
      Qrew Cadet
      No, I am using QuickBase's relative date ranges. IR is (Is During), and 'last wk' is a built in command with their API based on the documentation
    • UrsulaLl's avatar
      UrsulaLl
      Qrew Assistant Captain
      I was hoping that was not the case. I have had many issues with duration and date fields when running do_query, I had to do some funky stuff to get it to pull back correctly. I will be following this question to see if anyone comes up with a solution. Good luck!