-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdatetodo.js
59 lines (47 loc) · 1.82 KB
/
updatetodo.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
const fs = require('fs');
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
})
function updatetodo() {
if (fs.existsSync('./data.json')) {
fs.readFile('./data.json', 'utf8', (err, data) => {
if (err) {
console.log('Error From ReadFile ! ');
} else {
if (!data) {
console.log('Data Not Found ! ');
} else {
rl.question('Enter taskId ? : ', (taskId) => {
rl.question('Enter taskName ? : ', (taskName) => {
const todos = JSON.parse(data);
const update_todos = todos.map(todo => {
if (todo.id == Number(taskId)) {
return {
...todo,
task: taskName
}
}
return todo;
});
fs.writeFile('./data.json', JSON.stringify(update_todos, null, 2), (err) => {
if (err) {
console.log('Error Writting File ', err);
} else {
console.log('Successfully updated the task ');
}
})
console.log('update_todos', update_todos);
})
})
}
}
})
} else {
console.log(' Opps 404 ');
}
}
module.exports = {
updatetodo
}