Keyang / node-csvtojson

Blazing fast and Comprehensive CSV Parser for Node.JS / Browser / Command Line.
MIT License
2.02k stars 271 forks source link

question about using csvtojson with promises inside a class constructor. #204

Closed isaklafleur closed 7 years ago

isaklafleur commented 7 years ago

Hi

This is not an issue, rather a question how the library works.

I trying to create a class that have a couple of methods and I want to run these in a serie.

  1. read the dir
  2. read the files
  3. convert the files (csv) to json
  4. store it in MongoDb with help of Mongoose

I get this error: Unhandled rejection TypeError: cb is not a function at Converter.fromString (/Users/isaklafleur/Google...

Do you have any idea of how to fix this?

Code:

const Promise = require("bluebird");
const path = require("path");
const csv = require("csvtojson");
const fs = Promise.promisifyAll(require("fs"));

class ImportCSVFiles {
  constructor(directory, parserParameters, encoding) {
    this.directory = path.join(__dirname, directory);
    this.parserParameters = parserParameters;
    this.encoding = encoding;
  }
  getFiles() {
    return fs.readdirAsync(this.directory);
  }
  getContent(filename) {
    return fs
      .readFileAsync(this.directory + "/" + filename, this.encoding)
      .toString();
  }
  convertToJSON(content) {
    console.log("starting conversion");

    let i = 0;
    return new Promise((resolve, reject) => {
      console.log("setting up converter");
      csv(this.parserParameters)
        .fromString(content)
        .on("json", jsonObj => {
          console.log("Got json", jsonObj);
          i += 1;
        })
        .on("done", error => {
          console.log("done w/ error ", error);
          return resolve(i);
        });
    });
  }
  errorHandler(error) {
    if (error) throw error;
  }
}

const test = new ImportCSVFiles(
  "/data",
  {
    delimiter: [",", ";"],
    noheader: true,
    headers: [
      "facility",
      "item_number",
      "part_name",
      "part_description",
      "net_weight",
      "customs_statistical"
    ]
  },
  "utf8"
);

test
  .getFiles(files => console.log(files))
  .map(filename => {
    return test.getContent(filename);
  })
  .then(string => {
    test.convertToJSON(string);
  })
  .then(output => console.log(output))
  .catch(error => {
    test.errorHandler(error);
  });

Error:

starting conversion
setting up converter
undefined
Unhandled rejection TypeError: cb is not a function
    at Converter.fromString (/Users/isaklafleur/Google Drive/Isak/Coding/stringAlgo/node_modules/csvtojson/libs/core/Converter.js:510:12)
    at Promise (/Users/isaklafleur/Google Drive/Isak/Coding/stringAlgo/ImportCSVFiles.js:27:10)
    at Promise._execute (/Users/isaklafleur/Google Drive/Isak/Coding/stringAlgo/node_modules/bluebird/js/release/debuggability.js:300:9)
    at Promise._resolveFromExecutor (/Users/isaklafleur/Google Drive/Isak/Coding/stringAlgo/node_modules/bluebird/js/release/promise.js:483:18)
    at new Promise (/Users/isaklafleur/Google Drive/Isak/Coding/stringAlgo/node_modules/bluebird/js/release/promise.js:79:10)
    at ImportCSVFiles.convertToJSON (/Users/isaklafleur/Google Drive/Isak/Coding/stringAlgo/ImportCSVFiles.js:24:12)
    at test.getFiles.map.then.string (/Users/isaklafleur/Google Drive/Isak/Coding/stringAlgo/ImportCSVFiles.js:66:10)
    at tryCatcher (/Users/isaklafleur/Google Drive/Isak/Coding/stringAlgo/node_modules/bluebird/js/release/util.js:16:23)
    at Promise._settlePromiseFromHandler (/Users/isaklafleur/Google Drive/Isak/Coding/stringAlgo/node_modules/bluebird/js/release/promise.js:512:31)
    at Promise._settlePromise (/Users/isaklafleur/Google Drive/Isak/Coding/stringAlgo/node_modules/bluebird/js/release/promise.js:569:18)
    at Promise._settlePromise0 (/Users/isaklafleur/Google Drive/Isak/Coding/stringAlgo/node_modules/bluebird/js/release/promise.js:614:10)
    at Promise._settlePromises (/Users/isaklafleur/Google Drive/Isak/Coding/stringAlgo/node_modules/bluebird/js/release/promise.js:693:18)
    at Async._drainQueue (/Users/isaklafleur/Google Drive/Isak/Coding/stringAlgo/node_modules/bluebird/js/release/async.js:133:16)
    at Async._drainQueues (/Users/isaklafleur/Google Drive/Isak/Coding/stringAlgo/node_modules/bluebird/js/release/async.js:143:10)
    at Immediate.Async.drainQueues (/Users/isaklafleur/Google Drive/Isak/Coding/stringAlgo/node_modules/bluebird/js/release/async.js:17:14)
    at runCallback (timers.js:781:20)

csv example file:

SDC,4445210500,COVER,COVER,0,84314980
SDC,4445213000,SPRING,SPRING,0,84314980
SDC,4445213300,HOUSING,HOUSING,4,091,84314980
SDC,4445214700,WAGNER PART,BELL,1,048,84314980
SDC,4445214800,WAGNER PART,BELL,0,549,84314980
SDC,4445215300,RING,RING,0,84314980
SDC,4444467700,GEAR,GEAR,0,84834021
SDC,4444469200,HEAD,HEAD,0,84314980
SDC,4444469300,WIPER,WIPER,0,045,84314980
SDC,4444469500,SEAL,SEAL,0,045,84314980
SDC,4444469700,PISTON SYS,PISTON SYS,0,84314980
SDC,4444470000,O-RING-VITON,O-RING-VITON,0,045,84314980
SDC,4444470100,WAGNER PART,WASHER,0,045,73182200
SDC,4444470400,SHIM,SHIM,0,045,84314980
SDC,4444470600,SPRING,SPRING,0,113,84314980
SDC,4444470800,CAGE,CAGE,0,091,84314980
SDC,4444470900,ROD,ROD,0,113,84314980
D36,4444471500,ELEMENT,ELEMENT,0,068,8421230000
Keyang commented 7 years ago

try to console.log(content) in convertToJSON function . I think the content is passed wrong as fs.readFileAsync is not a function. Also .toString function from a Promise will not work -- you will need to do it after promising resolved.

~Keyang