Search code examples
javascriptarraysalasql

Alasql result to be used in other parts of the program


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.


Solution

  • 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);
        });