2

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

4
  • I think that your script has a problem. } is required to be removed from try{ do something }}}catch(e){ do something }. I think that this might be a miswriting. I think that when there are no syntax errors, alert(stIDInvData) has the returned value from getInvoicesForID(stID). If the same issue occurs even when the syntax error was removed, can you provide the script for replicating your issue? Of course, please remove your personal information.
    – Tanaike
    Commented Jul 5, 2019 at 3:12
  • I can't replicate your problem, for me is either not compiling the javascript code because as Tanaike said there is a syntax error with a bracket and if i delete it, the code works fine. Please provide the entire javascript.html. Commented Jul 5, 2019 at 8:50
  • Hi Tanaike, Andres Duarte I updated my question with a few more details, hopefully it might help. the original code compiles and I couldn't find any bracket mispairing. thank you for the comments. Commented Jul 5, 2019 at 11:26
  • Possible duplicate of Chart data exported to an Apps Script webapp is null
    – tehhowch
    Commented Jul 7, 2019 at 19:54

1 Answer 1

5

(...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

Not the answer you're looking for? Browse other questions tagged or ask your own question.