JavaScript API_DoQuery failing to give the proper xml response

  • 0
  • 1
  • Question
  • Updated 5 months ago
  • Answered
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).
Photo of John Barulich

John Barulich

  • 634 Points 500 badge 2x thumb

Posted 5 months ago

  • 0
  • 1
Photo of Ⲇanom the ultimate (Dan Diebolt)

Ⲇanom the ultimate (Dan Diebolt), Champion

  • 26,522 Points 20k badge 2x thumb
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.
Photo of John Barulich

John Barulich

  • 634 Points 500 badge 2x thumb
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
(Edited)
Photo of Ⲇanom the ultimate (Dan Diebolt)

Ⲇanom the ultimate (Dan Diebolt), Champion

  • 26,242 Points 20k badge 2x thumb
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.
Photo of John Barulich

John Barulich

  • 634 Points 500 badge 2x thumb
There has to be something I do not quite understand. How can my function return prior to the code being executed, when we are guaranteed by the promise to wait until the request is completed before execution can continue. The code within the function does not complete until that promise is fulfilled, so how does the function return prior to this?

I did test your solution but it returns the same result. Yet I can query all records with a specific field. Are we totally sure the backend implementation is correct?
(Edited)
Photo of John Barulich

John Barulich

  • 634 Points 500 badge 2x thumb
function getAndSetList() {
var url = "blahblah";
getPerm();
$.get(url, {
act: "API_DoQuery",
query: "{66.IR.'last wk'}",
clist: "66"
}).then(function(xml){
console.dirxml(xml);

}); /* console.log("Hi"); */
}


with the addition of the console.log("Hi"), I now see what you are saying. It treats the whole promise block as asynchronous as well, no?

But your solution does not work. Why? Can you test the IR.'last wk' on one of your tables as well and test?
(Edited)
Photo of Ursula Ll

Ursula Ll

  • 1,446 Points 1k badge 2x thumb
To add to the above comment, run the search as a URL, make sure that the search parameters match your do_query, it may be as simple as a missing space or quotation mark. If it returns empty or with an error, then something else is going on... http://target_domain/db/target_dbid?a=API_DoQuery&query={66.IR.'last wk'}

I have had issues in the past with duration fields, so it may be that the script is working as intended, but that field is being tempermental. 
 
Photo of John Barulich

John Barulich

  • 634 Points 500 badge 2x thumb
I figured out what my issue was. From the documentation:

"All records where the date or date/time in fid 7 occurs within the 7-day period...

XML example:

<query>{7.IR.'last wk'}</query>"

the FID is supposed to be the DATE field, and obviously can't be anything else but the DATE field. Of course you can add on subsequent fields with the 'clist' parameter
Photo of Ursula Ll

Ursula Ll

  • 1,446 Points 1k badge 2x thumb
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. 
Photo of John Barulich

John Barulich

  • 634 Points 500 badge 2x thumb
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
Photo of Ursula Ll

Ursula Ll

  • 1,446 Points 1k badge 2x thumb
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!
(Edited)