1

Trying to read a csv file and store data from it into an object which I can then use for other tasks.

    var csv = require('csv');

    var obj = csv();

    var filename = process.argv[2];

    function Employee(eno, ename, sal) {

        this.EmpNo = eno;
        this.EmpName = ename;
        this.Salary = sal;
    };

    var Employee = {};

    obj.from.path(filename).to.array(function (data) {
        for (var index = 0; index < data.length; index++) {
               Employees.push(new Employee(data[index][0], data[index][1], data[index][2]));
        }
    });

console.log(Employee);

When running this I get the error:

var obj = csv();
          ^

TypeError: csv is not a function

Any ideas?

Edit:

Changed to this as per answer below but cannot get the string to be populated outside the function:

string = {}

var lineReader = require('readline').createInterface({
    input: fs.createReadStream(filename)
});

lineReader.on('line', function (line) {
    var splitLine = line.split(',');

        string[splitLine[0]] = {
            name: splitLine[0],
            dob: splitLine[1],
            employeeid: splitLine[2],
            country: domain,
            zone: splitLine[4],
            type: splitLine[5],
            id: splitLine[7],
            number: splitLine[8]
        }

    lineReader.close()
    }
})

console.log(string)
2

1 Answer 1

4

Looking at the documentation for the module, it looks like you're not quite using it correctly. csv is not a constructor, but instead has methods which you should call. There are some examples available which should guide you towards correct usage.

Callback example from the module website;

var csv = require('csv');

csv.generate({seed: 1, columns: 2, length: 20}, function(err, data){
  csv.parse(data, function(err, data){
    csv.transform(data, function(data){
      return data.map(function(value){return value.toUpperCase()});
    }, function(err, data){
      csv.stringify(data, function(err, data){
        process.stdout.write(data);
      });
    });
  });
});

If you want to read from a file, and append the contents to an array;

Input

name,age
Alex,24
Sam,99

Code

var fs = require('fs');
var csv = require('csv');
var strings = [];

var readStream = fs.createReadStream('./input.csv');

var parser = csv.parse({columns:true});

parser.on('readable', function() {
  while(record = parser.read()) {
    strings.push(record);
  }
});

parser.on('error', function(err) {
  console.log(err.message);
});

parser.on('finish', (function() {
  console.log(strings);
}));

readStream.pipe(parser);

Output

[ { name: 'Alex', age: '24' }, { name: 'Sam', age: '99' } ]
Sign up to request clarification or add additional context in comments.

9 Comments

Okay thanks will take a look but I got the use of this from these sites which seemed to work: dotnetcurry.com/nodejs/1204/read-csv-file-using-nodejs csv.adaltas.com/legacy/from
@Sam That article is 2 years old, it could be that the API for csv has changed since then. Always good to check the docs!
Okay thanks Alex, using the example above can I write the data from a file store it into an object to use outside of the stream? If not would you know of a good library where I can do that and use lexical scoping?
You could use the fs module to read the file as a stream, and then stream-tranform by the maker of csv to append the objects to an array. You want fs.createReadStream('file.csv'); Personally, I use the pipe syntax.
I have used the fs.createReadStream(file.csv) before and from it just split the lines and stored into and object using that. But if I want to use that array outside of the lineReader method I can't as it's always empty which means I need to put all the logic within the lineReader.on method. Would you know a way I can use the array outside of the method scope?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.