0

In the code I have implemented a logic to read all the files in a ./contacts directory and create an array of all contacts(all vcf files) (each contact will be represented by an object having two properties name and number)

fs = require('fs');

var arr=[];

fs.readdir('./contacts', (err, data) => {
    data
    .map((c, i) => {
        var name;
        var path = './contacts/' + c;
        var syncData = fs.readFileSync(path, 'utf8');
        var number = syncData.split('\r\n')[4].split(';')[3].slice(6);
        if(c.indexOf('_') !== -1){
            name = c.slice(0,c.indexOf('_')).replace(/\s/g, '_');
        } else {
            name = c.slice(0,c.indexOf('.')).replace(/\s/g, '_');
        };      
        arr.push(new Factory(name, number));        
    });

});

function Factory(name, number){
    this.name = name.replace(/_/g," ");
    this.number = number;   
}

console.log(arr);

e.g. [{name:'someone', number:'digits'}, {name:'someone', number:'digits'}, .....] All the logic is working fine as expected but the array is empty. Please help.

chitra
  • 51
  • 1
  • 5

0 Answers0