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)