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

View this thread on: d.buzz | hive.blog | peakd.com | ecency.com
·@matthewdavid·
0.000 HBD
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
👍 , , , , , , , , , , , , , , ,