Reusing a variable from alasql

I am new to JS and I cannot reuse a variable from alasql. when i run my code i have "unidifined"

var dataSource = alasql('SELECT AGENT_NAME, count(*) FROM XLSX("export.xlsx",{headers:true}) GROUP BY AGENT_NAME')
console.log(dataSource)

      

but when i run

var dataSource = alasql('SELECT AGENT_NAME, count(*) FROM XLSX("export.xlsx",{headers:true}) GROUP BY AGENT_NAME',[],
        function (data) { console.log(data)})

      

everything is good

+3


source to share


1 answer


Alasql seems to have a callback after the database response. Try it.

var dataResult;
var dataSource = alasql('SELECT AGENT_NAME, count(*) FROM XLSX("export.xlsx",{headers:true}) GROUP BY AGENT_NAME',[],
        function (data) { dataResult = data })

      

dataResult will be undefined until the callback is called. After that, it will matter.



If you want to run any code after the database callback completes, put it in the callback itself.

var printResult = function(result){ console.log(result)};
var dataSource = alasql('SELECT AGENT_NAME, count(*) FROM XLSX("export.xlsx",{headers:true}) GROUP BY AGENT_NAME',[],
        function (data) { printResult(data); })

      

+1


source







All Articles