I am unsure why do I get undefined after I run console.log(class1.roster)
, even though I have passed an array to that object in the previous command class1.importRoster(createRoster(filePath));
. I would appreciate an explanation and a possible way to see class1.roster have a value. I am using the npm package csv-parser. Here is my MWE and the contents of test.csv
mwe.js
const fs = require ('fs');
const csv = require('csv-parser');
const filePath = 'test.csv'
let classRoster = [];
function schoolClass (subject, period, schoolYear) {
this.subject = subject;
this.period = +period;
this.schoolYear = schoolYear;
this.importRoster = function (roster) {
this.roster = roster;
}
}
function Student (firstName,lastName,ID,grade,period) {
this.firstName = firstName;
this.lastName = lastName;
this.ID = ID;
this.grade = grade;
this.period = period;
}
function createRoster (path) {
fs.createReadStream(path)
.pipe(csv())
.on('data', (data)=>{
classRoster.push(new Student(data.first_name, data.last_name, data.id, data.grade, data.period))
})
.on('end', ()=>{
console.log("Done");
return classRoster;
})
}
class1 = new schoolClass("GER", 3, "19-20");
console.log(class1);
class1.importRoster(createRoster(filePath));
console.log(class1.roster);
test.csv
first_name,last_name,grade,id,period
Blondy,Debrett,10,0217842058,3
Nester,Langstrath,10,2574570346,3
Waldon,Trunby,11,4785462996,8
Lark,Alsopp,11,0039229732,7
Almira,Baistow,12,1272978281,3
Carmela,Abberley,12,7279500295,8
Cristabel,Soanes,10,3086318686,5
Currie,Milton-White,11,8123679871,8
Alexei,Everist,11,2538149622,7
Lina,Hehir,9,1345944671,3