Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

My test does not work #14

Open
cscan opened this issue Nov 26, 2020 · 2 comments
Open

My test does not work #14

cscan opened this issue Nov 26, 2020 · 2 comments

Comments

@cscan
Copy link

cscan commented Nov 26, 2020

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)
@Avrmaster
Copy link

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

@cscan
Copy link
Author

cscan commented Mar 29, 2021

Thanks, will make test to it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants