简体   繁体   中英

google.script.run executes but it returns “null” while code.gs function does have/log values to return

Noob here, I've been with this problem for the past week and I can't understand why the javascript will not see return values from code.gs since I have the same code but different function working as expected.

Is there a reason why the code.gs function below log values in Logger.log meaning it has data to return, but javascript won't get any values and gives me a 'null' in the alert?

Code.gs

function getInvoicesForID(studentAltID) {
  var invoicesForID = [];
  //get general invoices sheet and values
  var sInvoices = ss.getSheetByName("Invoices");
  var dataInvoices = sInvoices.getDataRange().getValues();
  //get balance info for id
  for(var i = 0; i < dataInvoices.length; i++){
    if(dataInvoices[i][4]==studentAltID){
      invoicesForID.push([dataInvoices[i][0],dataInvoices[i][1],dataInvoices[i][2],dataInvoices[i][3]]);
      break;
    }
  }
  Logger.log("invoicesForID = " + invoicesForID);
  return invoicesForID;
}

javascript.html

  document.getElementById("btnsearchInvPayBalforStudentID").addEventListener("click",searchInvPayBalforStudentID);

//function to look for Payments, Invoices and Balance 

function searchInvPayBalforStudentID()
{
    try{
    //get the id 
      var stID = document.getElementById("txtStudentID").value;

          google.script.run.withSuccessHandler(getInvoices)
                           .getInvoicesForID(stID);
    }catch(e){
      alert(e);
    }
}
function getInvoices(stIDInvData) {
    try{
      alert(stIDInvData);
    }catch(e){
      alert(e);
    }
}

when the code is executed and I check the logs I do see data from my gs funtion that looks like this which is the data for the expected data for the studentAltID being passed

[19-07-04 22:12:13:491 EDT] invoicesForID = Thu Jan 31 2019 00:00:00 GMT-0500 (EST),34073,Matricula 2019,298854

what am I missing?

thank you in advance :)

UPDATE:

I included the event handler (button when clicked), I checked for syntax errors and bracket mispairing but I couldn't find the problem

Here's the link to the project, which contains a few more items which I cleaned out in my posting, hopefully it might help

https://script.google.com/d/1R2xgAOWslHWzsCCeEFCfilo0cSHNKmsDHw26YH-KTQ9JrpbYZnnVZ2mL/edit?usp=sharing

Thank you

(...beam of light shining through and angels singing Allelluyah in chorus in the background...)

I found the answer here: Unable to return Array [Google apps script] from @VishnarTadeleratha

as it turned out I can't pass dates in an array which is exactly one of the items in my array being returned: google says: "Parameters: ... Requests fail if you attempt to pass a Date, Function, DOM element besides a form, or other prohibited type, including prohibited types inside objects or arrays.... source: https://developers.google.com/apps-script/guides/html/reference/run

once I removed that item from my return array, javascript got values different than null.. hooray.

If I still need to pass the date in my array, which I do, @VishnarTadeleratha also suggested to convert that variable to a string and voila.. my long-ass week problem is now resolved... finally I can move on.. Cheers

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM