0

Hi I am trying to push data from my array of queries into excel but only the header seems to be displayed but the results are being pushed. When I did a console.log to see whether they would be pushed it says undefined. Any ideas as to why?

Example code:

var excel_data=[];
excel_data.push=(['Source', 'Count of thing 1', 'Count of thing 2', 'Count of thing 3']);
var database = new database(config);
var query_array = ['select count(*) as count_of_thing1 from table1', 'select count(*) as count_of_thing2 from table2', 'select count(*) as count_of_thing3 from table3'];

query_array.forEach(q => {
   database.query(q)
   .then(rows => {
        _.each(rows, function(row){
            excel_data.push([row.count_of_thing1, row.count_of_thing2, row.count_of_thing3]);
});
})
var wb = XLSX.utils.book_new();
 var ws_name = "Query_Results";
 var wscols = [ {width: 15} ];
 var wsrows = [];
 var ws = XLSX.utils.aoa_to_sheet(excel_data, {cellDates:true});
 ws['!cols'] = wscols;
 ws['!rows'] = wsrows;
 XLSX.utils.book_append_sheet(wb, ws, ws_name);
 var filenames = [
   ['mysql.xlsx', {bookSST:true}],
   ['mysql.csv', {bookSST:true}],
 ];
 filenames.forEach(function(r) {
   XLSX.writeFile(wb, r[0], r[1]);
   XLSX.readFile(r[0]);
 });

});
  • `excel_data` is empty when you're trying to write it to your file. `database.query` is an async operation, you'll have to wait until the queries are done. – Marcos Casagrande Apr 15 '18 at 14:59
  • [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Marcos Casagrande Apr 15 '18 at 15:01
  • so do I move the excel.push outside of the .each in order for the query to end and write to excel? – Usha Govindaraju Apr 15 '18 at 15:28

0 Answers0