-
Notifications
You must be signed in to change notification settings - Fork 591
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Class21/week2 #401
Class21/week2 #401
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great job. Good use of promises and nice code structure 🥇
if (error) { | ||
console.log(error); | ||
} | ||
const todoList = JSON.parse(toDos); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use of json 👍
@@ -0,0 +1,77 @@ | |||
const program = require('commander'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
commander 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It was very new, I hope I could do it properly.
week2/homework/bonus/bonus.js
Outdated
return fs.writeFile('./bonus/list.json', newList, error => console.log(error)); | ||
}); | ||
}) | ||
// The `update` takes only one argument, the second one is `undefined`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like a known issue. It is suggested to use program.args
tj/commander.js#53 (comment)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried this one but it didn't work at all for some reason.
program
.command('update')
.usage(' [new value...]')
.description('update one todo item')
.action(function(todoItem, newVal) {
fs.readFile('./bonus/list.json', 'utf8', (error, toDos) => {
if (error) {
console.log(error);
}
const todoList = JSON.parse(toDos);
const mappedList = todoList.map((elem, index) =>
index === Number(todoItem) - 2 ? newVal : elem,
);
const newList = JSON.stringify(mappedList, null, 2);
console.log(newList);
return fs.writeFile('./bonus/list.json', newList, error => console.log(error));
});
});
node bonus . update 2 "Do something else"
It does not make any changes and no messages at all in the console
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hmm, well no big deal, it is not your fault, it is an error in the library
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great!
week2/homework/bonus/bonus.js
Outdated
} | ||
const todoList = JSON.parse(toDos); | ||
const mappedList = todoList.map((elem, index) => | ||
index === Number(todoItem) - 2 ? newVal : elem, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why - 2 ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To reach todoItem
by its line number. The first line of the json
file is [
and the first todoItem
is line 2
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But you are using the index for the json array not for the line number in the file
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So -1
will be enough this way.
I'll do that.
|
||
// TODO: Write the homework code in this file | ||
const help = require('./help'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
separate files for each command 👍
list().then(toDos => console.log(toDos)); | ||
break; | ||
case 'add': | ||
add(todoItem).then(toDos => console.log(toDos)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice use of promises 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
function list() { | ||
return new Promise((resolve, reject) => { | ||
fs.readFile(fileName, 'utf8', (error, toDos) => { | ||
error ? reject(error) : resolve(toDos); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
error handling 👍
week2/homework/src/update.js
Outdated
|
||
function update(todoItem, newVal) { | ||
return new Promise((resolve, reject) => { | ||
list().then(toDos => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what if list throws an error?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The list()
handles errors
itself and the writeFile
handles it as well, is not that enough?!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
list() rethrows the error, so it needs to be handled here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So I have to use the .catch
to solve it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
`.catch(error => console.log(error));`
const fs = require('fs'); | ||
const fileName = './src/data.json'; | ||
|
||
function list() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
separate function for loading the file 👍
No description provided.