-
Notifications
You must be signed in to change notification settings - Fork 0
/
listing.js
44 lines (35 loc) · 1.09 KB
/
listing.js
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
let $ = require('jquery');
let fs = require('fs');
let filename = 'contacts';
let sno = 0;
$('#add-to-list').on('click', () => {
let name = $('#Name').val();
let email = $('#Email').val();
fs.appendFile('contacts', name + ',' + email + '\n');
addEntry(name, email);
});
function addEntry(name, email) {
if (name && email) {
sno++;
let updateString = '<tr><td>' + sno + '</td><td>' + name + '</td><td>' +
email + '</td></tr>';
$('#contact-table').append(updateString);
}
};
function loadAndDisplayContacts() {
//Check if file exists
if (fs.existsSync(filename)) {
let data = fs.readFileSync(filename, 'utf8').split('\n');
data.forEach((contact, index) => {
let [name, email] = contact.split(',');
addEntry(name, email);
});
} else {
console.log("File Doesn\'t Exist. Creating new file.");
fs.writeFile(filename, '', (err) => {
if (err)
console.log(err);
})
}
}
loadAndDisplayContacts();