create account

Saving Notes to a JSON file: Day Six of "The Complete Node.js Developer Course" by matthewdavid

View this thread on: hive.blogpeakd.comecency.com
· @matthewdavid ·
$1.42
Saving Notes to a JSON file: Day Six of "The Complete Node.js Developer Course"
![photo5005946566204303301 Cropped.jpg](https://steemitimages.com/DQmT3GZ3apYK6gf19WDrGXSmZo2D4Ts65Y3LpojqojEcrqf/photo5005946566204303301%20Cropped.jpg)

# After a few days off, I'm resuming my Node.js studies

Back at it. I was planning on programming and writing about it both Monday and Tuesday, but life happened. What I've learned in this process of learning programming is that daily practice is really important. If you miss a couple days, just get back to it. Don't let a few days turn into a week!

## Previously
* [My Programming Goals: javascript proficiency, node.js and databases](https://steemit.com/programming/@matthewdavid/my-programming-goals-javascript-proficiency-node-js-and-databases)
* [My Programming Progress: Day One of "The Complete Node.js Developer Course"](https://steemit.com/javascript/@matthewdavid/my-programming-progress-day-one-of-the-complete-node-js-developer-course)
* [My Programming Progress: Day Two of the Complete Node.js Developer Course - require](https://steemit.com/javascript/@matthewdavid/my-programming-progress-day-two-of-the-complete-node-js-developer-course-require)
* [Using 3rd Party Modules :: Day Three of "The Complete Node.js Developer Course"](https://steemit.com/javascript/@matthewdavid/using-3rd-party-modules-day-three-of-the-comple-node-js-developer-course)
* [My Programming Progress: Day Four of "The Complete Node.js Developer Course"](https://steemit.com/javascript/@matthewdavid/my-programming-progress-day-four-of-the-complete-node-js-developer-course)
* [JSON, A Brief Explanation: Day Five of "The Complete Node.js Developer Course"](https://steemit.com/javascript/@matthewdavid/json-a-brief-explanation-day-five-of-the-complete-node-js-developer-course)


## Saving notes to a JSON file

This section **16. Adding and Saving Notes** was all about using the ```addNote``` function to take in parameters from the command line for a note title and body and then write them to a **notes-data.json** file.

We did require the ```fs``` built-in module from node with: 

```const fs = require('fs');```

Here's the completed code from the **addNote** function for this section:

```
var addNote = (title, body) => {
  var notes = [];
  var note = {
    title,
    body
  }

  try {
    var notesString = fs.readFileSync('notes-data.json')
    notes = JSON.parse(notesString)
  } catch (e) {

  }

  var duplicateNotes = notes.filter((note) => note.title === title)

  if (duplicateNotes.length === 0) {
    notes.push(note);
    fs.writeFileSync('notes-data.json', JSON.stringify(notes))
  }
}
```

### Learning new things
There were a few things that I hadn't encountered before. I hadn't used the "try, catch" logic before to deal with errors. Essentially, the code in "try" will be tried. If there are no errors, then the code will run. If there are errors, like the "notes-data.json" file not existing yet, then that bit of code won't run. 

Also, I hadn't used the ```.filter``` method. I'm still not entirely clear on how this bit of code works: 

```var duplicateNotes = notes.filter((note) => note.title === title)``` 

But I understand what it does. It checks to see if the incoming note has the same title as any of the previously saved notes. If it does have the same title, it adds that to an array called ```duplicateNotes```. The next bit of code checks to see if the length of that array is zero. If it is zero, then the incoming note title isn't the same as any of the other previously saved notes. That checked, the note is written to the file. If  the array ```duplicateNotes``` has a length of anything other than zero, the incoming note is not written to the file.

## Progress!

I'm feeling like I want to be making faster progress, but it's undeniable that I am making progress. I have to remind myself that I can't learn complex skills in a day. It takes time.

It has also been good for me to write these posts. By writing an explanation of what I've learned, I confirm to myself that I understand it (or at least a good deal of it).

## Other node.js learning

I was very happy to attend a local meetup this evening where the topic was REST APIs with node.js and express. I was happy that I understood a good deal of what was presented. Also glad that the code presented in the talk was saved to a github repository that I can go back to and reference when I start to build my own apps.

### Thanks for reading!

--- @matthewdavid
👍  , , , , , , , , , , , , , ,
properties (23)
authormatthewdavid
permlinksaving-notes-to-a-json-file-day-six-of-the-complete-node-js-developer-course
categoryjavascript
json_metadata{"tags":["javascript","programming","nodejs","technology","learning"],"users":["matthewdavid"],"image":["https://steemitimages.com/DQmT3GZ3apYK6gf19WDrGXSmZo2D4Ts65Y3LpojqojEcrqf/photo5005946566204303301%20Cropped.jpg"],"links":["https://steemit.com/programming/@matthewdavid/my-programming-goals-javascript-proficiency-node-js-and-databases","https://steemit.com/javascript/@matthewdavid/my-programming-progress-day-one-of-the-complete-node-js-developer-course","https://steemit.com/javascript/@matthewdavid/my-programming-progress-day-two-of-the-complete-node-js-developer-course-require","https://steemit.com/javascript/@matthewdavid/using-3rd-party-modules-day-three-of-the-comple-node-js-developer-course","https://steemit.com/javascript/@matthewdavid/my-programming-progress-day-four-of-the-complete-node-js-developer-course","https://steemit.com/javascript/@matthewdavid/json-a-brief-explanation-day-five-of-the-complete-node-js-developer-course"],"app":"steemit/0.1","format":"markdown"}
created2017-09-07 04:56:57
last_update2017-09-07 04:56:57
depth0
children10
last_payout2017-09-14 04:56:57
cashout_time1969-12-31 23:59:59
total_payout_value1.150 HBD
curator_payout_value0.272 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length4,424
author_reputation2,155,255,892,877
root_title"Saving Notes to a JSON file: Day Six of "The Complete Node.js Developer Course""
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id14,114,642
net_rshares445,548,190,554
author_curate_reward""
vote details (15)
@kkomaz ·
$0.05
Awesome share Matt! (or do you go by Matthew?)

```
var duplicateNotes = notes.filter((note) => note.title === title)
```

Your explanation of how it works is correct.  Since notes contains a list of javascript object like 

```
{
  title: 'Some String'
  body: 'whatever'
}
```

It basically is "filter"-ing out from your array of objects that matches those conditions... in this case "title".

The ```((note))``` part is arbitrary.  You could have put ```apple``` and do the comparison like ```apple.title === title```.

It is not related to the ```note```  variable you created initially in the addNote function.  Hope that helps :)
👍  ,
properties (23)
authorkkomaz
permlinkre-matthewdavid-saving-notes-to-a-json-file-day-six-of-the-complete-node-js-developer-course-20170907t055431957z
categoryjavascript
json_metadata{"tags":["javascript"],"app":"steemit/0.1"}
created2017-09-07 05:54:30
last_update2017-09-07 05:54:30
depth1
children2
last_payout2017-09-14 05:54:30
cashout_time1969-12-31 23:59:59
total_payout_value0.037 HBD
curator_payout_value0.009 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length635
author_reputation15,393,882,786
root_title"Saving Notes to a JSON file: Day Six of "The Complete Node.js Developer Course""
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id14,118,188
net_rshares14,785,098,301
author_curate_reward""
vote details (2)
@matthewdavid ·
Thanks, that does help. I appreciate it. 

I do go by Matthew.
properties (22)
authormatthewdavid
permlinkre-kkomaz-re-matthewdavid-saving-notes-to-a-json-file-day-six-of-the-complete-node-js-developer-course-20170908t031730589z
categoryjavascript
json_metadata{"tags":["javascript"],"app":"steemit/0.1"}
created2017-09-08 03:17:30
last_update2017-09-08 03:17:30
depth2
children1
last_payout2017-09-15 03:17:30
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length62
author_reputation2,155,255,892,877
root_title"Saving Notes to a JSON file: Day Six of "The Complete Node.js Developer Course""
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id14,211,484
net_rshares0
@woz.software ·
I am not to up on JavaScript lambda but is this valid, as in no brackets around the param. I know C# only requires the brackets when more than one argument into the lambda.

```
var duplicateNotes = notes.filter(note => note.title === title)
```

As you can see, I am a clean code junkie lol The less characters while remaining readable is always better :)
properties (22)
authorwoz.software
permlinkre-matthewdavid-re-kkomaz-re-matthewdavid-saving-notes-to-a-json-file-day-six-of-the-complete-node-js-developer-course-20170908t063047727z
categoryjavascript
json_metadata{"tags":["javascript"],"app":"steemit/0.1"}
created2017-09-08 06:30:48
last_update2017-09-08 06:30:48
depth3
children0
last_payout2017-09-15 06:30:48
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length356
author_reputation2,321,910,395,519
root_title"Saving Notes to a JSON file: Day Six of "The Complete Node.js Developer Course""
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id14,223,114
net_rshares0
@phil-coding ·
$0.03
Again excellent work  I can't wait for the next post
👍  ,
properties (23)
authorphil-coding
permlinkre-matthewdavid-saving-notes-to-a-json-file-day-six-of-the-complete-node-js-developer-course-20170907t101427154z
categoryjavascript
json_metadata{"tags":["javascript"],"app":"steemit/0.1"}
created2017-09-07 10:16:03
last_update2017-09-07 10:16:03
depth1
children1
last_payout2017-09-14 10:16:03
cashout_time1969-12-31 23:59:59
total_payout_value0.026 HBD
curator_payout_value0.006 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length52
author_reputation4,773,758,818
root_title"Saving Notes to a JSON file: Day Six of "The Complete Node.js Developer Course""
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id14,135,548
net_rshares10,382,855,738
author_curate_reward""
vote details (2)
@matthewdavid ·
Thanks! I'll keep pushing myself to code and write about it. Thanks for the encouragement.
properties (22)
authormatthewdavid
permlinkre-phil-coding-re-matthewdavid-saving-notes-to-a-json-file-day-six-of-the-complete-node-js-developer-course-20170908t032159905z
categoryjavascript
json_metadata{"tags":["javascript"],"app":"steemit/0.1"}
created2017-09-08 03:22:00
last_update2017-09-08 03:22:00
depth2
children0
last_payout2017-09-15 03:22:00
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length90
author_reputation2,155,255,892,877
root_title"Saving Notes to a JSON file: Day Six of "The Complete Node.js Developer Course""
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id14,211,761
net_rshares0
@woz.software ·
$0.03
Good to see nice consistent code layout :) Makes it easier to digest code when it feels familiar.

Eating exceptions like this is not a good idea, you never know if it was failure in the reading or something else. 

Not sure the best solution for this bit, could even generate a note with the failure info?

It is the sort of thing that comes back later to bite you. 

hth

Woz
```
try {
    var notesString = fs.readFileSync('notes-data.json')
    notes = JSON.parse(notesString)
  } catch (e) {

  }
```
👍  
properties (23)
authorwoz.software
permlinkre-matthewdavid-saving-notes-to-a-json-file-day-six-of-the-complete-node-js-developer-course-20170907t063203129z
categoryjavascript
json_metadata{"tags":["javascript"],"app":"steemit/0.1"}
created2017-09-07 06:32:03
last_update2017-09-07 06:32:03
depth1
children4
last_payout2017-09-14 06:32:03
cashout_time1969-12-31 23:59:59
total_payout_value0.022 HBD
curator_payout_value0.007 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length505
author_reputation2,321,910,395,519
root_title"Saving Notes to a JSON file: Day Six of "The Complete Node.js Developer Course""
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id14,120,526
net_rshares9,511,193,108
author_curate_reward""
vote details (1)
@jfuenmayor96 · (edited)
> Eating exceptions like this is not a good idea, you never know if it was failure in the reading or something else.

I agree with this. Maybe you can try making it fail on purpose (trying to open a file that doesn't exist for example) and then `console.log(e)` so you can see what is returned. Normally when I catch errors I do `console.log(e.stack)`, and it gives enough explanation of what happened. 

Also, I learned how to use `.filter()` with your post and the further explanation that @kkomaz wrote.
properties (22)
authorjfuenmayor96
permlinkre-wozsoftware-re-matthewdavid-saving-notes-to-a-json-file-day-six-of-the-complete-node-js-developer-course-20170910t220027175z
categoryjavascript
json_metadata{"tags":["javascript"],"app":"steemit/0.1","users":["kkomaz"]}
created2017-09-10 22:00:27
last_update2017-09-10 22:03:54
depth2
children0
last_payout2017-09-17 22:00:27
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length506
author_reputation1,765,327,312,641
root_title"Saving Notes to a JSON file: Day Six of "The Complete Node.js Developer Course""
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id14,497,096
net_rshares0
@matthewdavid ·
I see what you are saying with this and I thought about it when going through this exercise. This section in the course left the code like this, but it makes sense to give some feedback if an error occurred. I think the next section will be refactoring this bit of code so that the logic can be reused by other functions. We'll see when I get there.
👍  
properties (23)
authormatthewdavid
permlinkre-wozsoftware-re-matthewdavid-saving-notes-to-a-json-file-day-six-of-the-complete-node-js-developer-course-20170908t032049695z
categoryjavascript
json_metadata{"tags":["javascript"],"app":"steemit/0.1"}
created2017-09-08 03:20:48
last_update2017-09-08 03:20:48
depth2
children2
last_payout2017-09-15 03:20:48
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length349
author_reputation2,155,255,892,877
root_title"Saving Notes to a JSON file: Day Six of "The Complete Node.js Developer Course""
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id14,211,696
net_rshares5,807,306,112
author_curate_reward""
vote details (1)
@woz.software ·
Will be interesting to see what they propose is the correct way after the refactor :)
properties (22)
authorwoz.software
permlinkre-matthewdavid-re-wozsoftware-re-matthewdavid-saving-notes-to-a-json-file-day-six-of-the-complete-node-js-developer-course-20170908t062815476z
categoryjavascript
json_metadata{"tags":["javascript"],"app":"steemit/0.1"}
created2017-09-08 06:28:15
last_update2017-09-08 06:28:15
depth3
children0
last_payout2017-09-15 06:28:15
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length85
author_reputation2,321,910,395,519
root_title"Saving Notes to a JSON file: Day Six of "The Complete Node.js Developer Course""
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id14,222,930
net_rshares0
@woz.software ·
One other thing I noticed is there is no try/catch around the writing of the file so if that fails for whatever reason it will bubble up while the read is caught. Was that intentional?
properties (22)
authorwoz.software
permlinkre-matthewdavid-re-wozsoftware-re-matthewdavid-saving-notes-to-a-json-file-day-six-of-the-complete-node-js-developer-course-20170908t063211023z
categoryjavascript
json_metadata{"tags":["javascript"],"app":"steemit/0.1"}
created2017-09-08 06:32:12
last_update2017-09-08 06:32:12
depth3
children0
last_payout2017-09-15 06:32:12
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length184
author_reputation2,321,910,395,519
root_title"Saving Notes to a JSON file: Day Six of "The Complete Node.js Developer Course""
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id14,223,218
net_rshares0