0

I have imported an XLSX file but i cannot seem to use the result outside its function:

var dataResult;


alasql('select * from xlsx("adat.xlsx",{headers:true, sheetid:"adat", range:"A1:B21"})',
         [],function(data) {
               dataResult= data;
         });

 console.log(dataResult);

The result of the console.log is "undefined"

Could someone help me solve this problem? Thank you in advance.

elwindly
  • 45
  • 1
  • 7

1 Answers1

1

You should use a promise instead. The console.log is called before the result has been obtained.

var dataResult;
var checkResults = function () {
    console.log(dataResult);
}
alasql
.promise('select * from xlsx("adat.xlsx",{headers:true, sheetid:"adat", range:"A1:B21"}))
    .then(function (res) {
        dataResult = res;
        checkResults();
    }).catch(function (err) {
        console.log('error:', err);
    });
Rob Warren
  • 26
  • 2