0

I am an amateur programmer having a hard time with a little project. I would like to get a data from a Google spreadsheet, modify the data, and make a Google Chart (using google chart api).

The below code works perfectly in Firefox but Chrome and IE don't allow the dataTable outside the scope of the handleQueryResponse(e) function. My first thought was to return the dataTable from the handleQueryResponse(e) function but that is called inside of a function (.send()).

Any help would be greatly appreciated.

function getDataTable(link) {
     var dataTable= new google.visualization.DataTable();
     var queryTrip = new google.visualization.Query(link);
     queryTrip.send(handleQueryResponse);
     function handleQueryResponse(response) {

         if (response.isError()) {

             alert('Error in query: ' + response.getMessage() + ' ' + response.getDetailedMessage());
             return;
         }

         dataTable = response.getDataTable();
         // I can verify this has the data through debugging in Chrome  
     }
     // Outside of the function handleQueryResponse the dataTable is gone
    return dataTable;
 }
6
  • 5
    I assume queryTrip.send is asynchronous. In that case you would return dataTable from your function before it was set by the callback. That is return dataTable; is executed before dataTable = response.getDataTable();. You cannot return a value from a function if asynchronous function calls are involved. You should make getDataTable accept a callback which you call ones the result is ready and pass it to the callback. Commented May 9, 2012 at 15:31
  • "allow" makes no sense in this context. All browsers allow to define variables everywhere. Please be more specific: Do you get an error somewhere? If so, tell us the whole error message! Commented May 9, 2012 at 15:32
  • 3
    @JoshMein it's perfectly normal to declare functions inside other functions in Javascript. Commented May 9, 2012 at 15:33
  • @JoshMein — Is there a reason that function needs to be available in a wider scope? Commented May 9, 2012 at 15:33
  • I have just never seen it done before and it sort of confused me as of why you would want to. But thanks to your comments, I think I understand a little futher now. Commented May 9, 2012 at 15:36

2 Answers 2

3

Functions that call asynchronous functions (queryTrip.send) cannot return data which depends on the result of that asynchronous function call, nor otherwise use the data.

That data is only available inside the callback function, and any other function subsequently called by that callback.

Sign up to request clarification or add additional context in comments.

Comments

0

as felix mentioned, it's probably asynchronous, meaning that variable no longer exists by the time the handleQueryResponse gets run because it's not run instantly within the scope of the function getDataTable.

You could put the variable for dataTable outside that scope ( into global scope ) than the handleQueryResponse would be able to write to it.

However you will want to initiate any functions that need to run after the dataTable is ready from within the handleQueryResponse function. Best course of action might be to use some sort of event driven handling for after the dataTable is ready.

1 Comment

The problem is not where dataTable is defined. The problem is when it is read and set... in this case it is read (returned) before it was set.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.