0

I'm new to NodeJS+JavaScript. In the following JS the excelData returns a Promise when require from another JS. In the importing JS I can resolve and get it's value. But without exporting a promise, how to export the array directly?

I need this because I have a JS where promises are not supported.

var conf = require('../config');
var excel = require('exceljs');

var workbook = new excel.Workbook();

var excelData = workbook.xlsx.readFile(conf.intentExcel).then(function() {
 var customerSheet = workbook.getWorksheet(conf.customerDataSheet);
 var excelRows = [];
 customerSheet.eachRow({
  includeEmpty : true
 }, function(row, rowNumber) {
  if (JSON.stringify(row.values[1])) {
   var rowObj = {
    intent : JSON.stringify(row.values[1]),
    level : JSON.stringify(row.values[2]),
    optLvl1 : JSON.stringify(row.values[3]),
    optLvl2 : JSON.stringify(row.values[4]),
    optLvl3 : JSON.stringify(row.values[5]),
    questionId : JSON.stringify(row.values[6]),
    question : JSON.stringify(row.values[7]),
    expAnswerId : JSON.stringify(row.values[8])
   }
   for ( var key in rowObj) {
    if (rowObj[key] != null) {
     rowObj[key] = rowObj[key].replace(/['"]+/g, '');
    }
   }
   excelRows.push(rowObj);
  }
 });
 return excelRows;
});

module.exports = {
 excelData : excelData
};
Umesh_IoT
  • 59
  • 1
  • 11
  • You can't because because `workbook.xlsx.readFile` is an asynchronous process. Another method to handle asynchronous process is using `callback` but I don't think `exceljs` support callback. – iKoala Nov 25 '16 at 12:23

2 Answers2

2

I have a callback wrapper for you but I don't know if this is helpful.

var getDataFromExcel = function(callback) {
    var excelData = workbook.xlsx.readFile(conf.intentExcel).then(function() {
        var customerSheet = workbook.getWorksheet(conf.customerDataSheet);
        var excelRows = [];
        customerSheet.eachRow({
            includeEmpty : true
        }, function(row, rowNumber) {
            if (JSON.stringify(row.values[1])) {
                var rowObj = {
                    intent : JSON.stringify(row.values[1]),
                    level : JSON.stringify(row.values[2]),
                    optLvl1 : JSON.stringify(row.values[3]),
                    optLvl2 : JSON.stringify(row.values[4]),
                    optLvl3 : JSON.stringify(row.values[5]),
                    questionId : JSON.stringify(row.values[6]),
                    question : JSON.stringify(row.values[7]),
                    expAnswerId : JSON.stringify(row.values[8])
                }
                for ( var key in rowObj) {
                    if (rowObj[key] != null) {
                        rowObj[key] = rowObj[key].replace(/['"]+/g, '');
                    }
                }
                excelRows.push(rowObj);
            }
        });
        callback(null, excelRows);
    }).catch(function(err) {
        callback(err, null);
    });
};

module.exports = {
    getDataFromExcel : getDataFromExcel
};

Usage:

require('excel-module').getDataFromExcel(function(err, data) {
  if (err) {
    //handle error
    return;
  }
  //handle data
});
iKoala
  • 870
  • 4
  • 11
1

You need to export a function that have a callback, where you pass the data. Once you have this, you use it in the following way:

MyAwesomeImportedModule.excelData(function(data) {

    console.log(data)

});
var conf = require('../config');
var excel = require('exceljs');

var workbook = new excel.Workbook();

var excelData = function(callback) {
    workbook.xlsx.readFile(conf.intentExcel).then(function() {

        var customerSheet = workbook.getWorksheet(conf.customerDataSheet);

        var excelRows = [];

        customerSheet.eachRow({
            includeEmpty : true
        }, function(row, rowNumber) {

            if (JSON.stringify(row.values[1])) {

                var rowObj = {
                    intent : JSON.stringify(row.values[1]),
                    level : JSON.stringify(row.values[2]),
                    optLvl1 : JSON.stringify(row.values[3]),
                    optLvl2 : JSON.stringify(row.values[4]),
                    optLvl3 : JSON.stringify(row.values[5]),
                    questionId : JSON.stringify(row.values[6]),
                    question : JSON.stringify(row.values[7]),
                    expAnswerId : JSON.stringify(row.values[8])
                }

                for ( var key in rowObj) 
                {
                    if (rowObj[key] != null) 
                    {
                        rowObj[key] = rowObj[key].replace(/['"]+/g, '');
                    }
                }

                excelRows.push(rowObj);
            }

        });

        callback(excelRows);

    });

}

module.exports = {
    excelData : excelData
};
David Gatti
  • 3,576
  • 3
  • 33
  • 64