Tuesday 14 March 2017

NPM package excel-as-json fires callback early

I am using excel-as-json NPM package to convert an excel file to json (https://www.npmjs.com/package/excel-as-json).

As per the documentation the last parameter is the callback function. So, I wrote this:

convertExcel = require('excel-as-json').processFile;


function fixJson(fileName){

    var jsonfile = require('jsonfile');
    var file = fileName;

    jsonfile.readFile(file, function(err, data) {
        if(err){
            console.log(err);
        }
    })



}

convertExcel('test.xlsx', 'test.json', {sheet:"1"}, fixJson('test.json'));

But when I run this file through node, the callback function fixJson() fires before the conversion completes. Due to this I get an error "Unexpected end of JSON input". When I look at the JSON file, it contains the data.

Am I doing something wrong here? or there is a problem with the callback function?

Thanks.



via Blueboye

No comments:

Post a Comment