-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparseCsv.js
More file actions
26 lines (26 loc) · 877 Bytes
/
Copy pathparseCsv.js
File metadata and controls
26 lines (26 loc) · 877 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
const csv = require('csvtojson')
const path = require('path')
const fs = require('fs')
const contacts = []
// fs.readFile(path.join(__dirname, 'src/dbs/contacts.csv'), (err, data) => {
// if (err) throw err;
csv({ noheader: true })
.fromFile(path.join(__dirname, 'src/dbs/contacts.csv'))
.on('json', (json) => { //this func will be called 3 times
contacts.push({
username: json.field2,
birthday: json.field3,
address: json.field4,
job: json.field5,
jobAddress: json.field6,
mobile: json.field7,
status: json.field8
});
})
.on('done', () => {
fs.writeFile(path.join(__dirname, 'src/dbs/contacts.json'), JSON.stringify(contacts), 'utf8', (err) => {
if (err) throw err;
console.log('contacts data saved!');
});
})
// })