Skip to content
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

Closed
wants to merge 8 commits into from
Closed

Class21/week2 #401

wants to merge 8 commits into from

Conversation

ghifo
Copy link

@ghifo ghifo commented Jul 11, 2019

No description provided.

Copy link
Contributor

@gajduk gajduk left a 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);
Copy link
Contributor

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');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

commander 👍

Copy link
Author

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.

return fs.writeFile('./bonus/list.json', newList, error => console.log(error));
});
})
// The `update` takes only one argument, the second one is `undefined`.
Copy link
Contributor

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)

Copy link
Author

@ghifo ghifo Jul 13, 2019

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

Copy link
Contributor

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

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great!

}
const todoList = JSON.parse(toDos);
const mappedList = todoList.map((elem, index) =>
index === Number(todoItem) - 2 ? newVal : elem,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why - 2 ?

Copy link
Author

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

Copy link
Contributor

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

Copy link
Author

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');
Copy link
Contributor

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));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice use of promises 👍

Copy link
Author

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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

error handling 👍


function update(todoItem, newVal) {
return new Promise((resolve, reject) => {
list().then(toDos => {
Copy link
Contributor

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?

Copy link
Author

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?!

Copy link
Contributor

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

Copy link
Author

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

Copy link
Author

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() {
Copy link
Contributor

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 👍

@prof-xed prof-xed closed this Aug 13, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants