node-excel-stream icon indicating copy to clipboard operation
node-excel-stream copied to clipboard

My test does not work

Open cscan opened this issue 4 years ago • 2 comments

My code:

var fs = require('fs');
var ExcelWriter = require('node-excel-stream').ExcelWriter;

var inputs = [
    {name: 'Test 1', testValue: 100},
    {name: 'Test 2', testValue: 90},
    {name: 'Test 3', testValue: 80}
]

let writer = new ExcelWriter({
    sheets: [{
        name: 'Test Sheet',
        key: 'tests',
        headers: [{
            name: 'Test Name',
            key: 'name'
        }, {
            name: 'Test Coverage',
            key: 'testValue',
            default: 0
        }]
    }]
});

let dataPromises = inputs.map((input) => {
    // 'tests' is the key of the sheet. That is used
    // to add data to only the Test Sheet
    writer.addData('tests', input);
});
Promise.all(dataPromises)
.then(() => {
    return writer.save();
})
.then((stream) => {
    stream.pipe(fs.createWriteStream('data.xlsx'));
});

Output is:

$ node test
(node:68391) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'length' of null
    at WorksheetWriter.get _nextRow [as _nextRow] (/Users/can/ortholite/batch/node_modules/exceljs/lib/stream/xlsx/worksheet-writer.js:334:39)
    at WorksheetWriter.addRow (/Users/can/ortholite/batch/node_modules/exceljs/lib/stream/xlsx/worksheet-writer.js:400:36)
    at afterInit.then.then (/Users/can/ortholite/batch/node_modules/node-excel-stream/src/excel-writer.js:98:17)
    at process._tickCallback (internal/process/next_tick.js:68:7)
    at Function.Module.runMain (internal/modules/cjs/loader.js:834:11)
    at startup (internal/bootstrap/node.js:283:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)
(node:68391) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:68391) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
(node:68391) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'length' of null
    at WorksheetWriter.get _nextRow [as _nextRow] (/Users/can/ortholite/batch/node_modules/exceljs/lib/stream/xlsx/worksheet-writer.js:334:39)
    at WorksheetWriter.addRow (/Users/can/ortholite/batch/node_modules/exceljs/lib/stream/xlsx/worksheet-writer.js:400:36)
    at afterInit.then.then (/Users/can/ortholite/batch/node_modules/node-excel-stream/src/excel-writer.js:98:17)
    at process._tickCallback (internal/process/next_tick.js:68:7)
    at Function.Module.runMain (internal/modules/cjs/loader.js:834:11)
    at startup (internal/bootstrap/node.js:283:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)
(node:68391) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2)
(node:68391) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'length' of null
    at WorksheetWriter.get _nextRow [as _nextRow] (/Users/can/ortholite/batch/node_modules/exceljs/lib/stream/xlsx/worksheet-writer.js:334:39)
    at WorksheetWriter.addRow (/Users/can/ortholite/batch/node_modules/exceljs/lib/stream/xlsx/worksheet-writer.js:400:36)
    at afterInit.then.then (/Users/can/ortholite/batch/node_modules/node-excel-stream/src/excel-writer.js:98:17)
    at process._tickCallback (internal/process/next_tick.js:68:7)
    at Function.Module.runMain (internal/modules/cjs/loader.js:834:11)
    at startup (internal/bootstrap/node.js:283:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)
(node:68391) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 3)

cscan avatar Nov 26 '20 13:11 cscan

@cscan @amrakun It looks like addData is an asynchronous method that returns Promise that has to be awaited. The error is occursing because you're trying to add data after the "save" method was called. Thus, the return should solve an issue, as you would save only after all of the data has been added.

let dataPromises = inputs.map((input) => {
    // 'tests' is the key of the sheet. That is used
    // to add data to only the Test Sheet
    return writer.addData('tests', input);
});

Avrmaster avatar Mar 28 '21 15:03 Avrmaster

Thanks, will make test to it.

cscan avatar Mar 29 '21 04:03 cscan