Reuse variable from alasql

1.1k views Asked by At

I am newest in JS and I can not reuse 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 fine

1

There are 1 answers

1
aadarshsg On BEST ANSWER

Seems like alasql has a callback after database response. Try this.

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 till the callback is triggered. After that, it will have the value.

If you want to trigger any code after the database callback is done, 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); })