3.9 Adding and Saving Notes

We will learn how to apply JSON in our notes app.

Using fs

We need to truly persist data. Now we start by learning store them in file system. It is important to have fs functionality.

const fs = require('fs');

Updating

Previously, functions like addNote only print dummy texts. Today we truly do something real, i.e. persisting notes.

Change in addNote

var addNote = (title, body) => {
var notes = [];
var note = {
title, // same as title: title
body
};
notes.push(note);
fs.writeFileSync('notes-data.json', JSON.stringify(notes));
};

Remember doing so will override the exsisting ones. This example is only to demonstrate whether data were successfully persisted in our filesystem.

Output:

jude@jude-ThinkPad-S5:/media/jude/Jude 1TB/nodejs/notes-app$ node app.js add --title="cool" --body="persist"

We will find that a new file is generated.
In the file just generated, notes-data.json, it stores exactly what users typed into add function:

[{"title":"cool","body":"persist"}]

Fix Overriding

var addNote = (title, body) => {
var notes=[];
var note = {
title, // same as title: title
body
};
var notesString = fs.readFileSync('notes-data.json');
notes = JSON.parse(notesString);
notes.push(note);
fs.writeFileSync('notes-data.json', JSON.stringify(notes));
};

However, if notes-data.json were not in that current directory, the program would crash.

Fix Crashing due to File not Found

try {
var notesString = fs.readFileSync('notes-data.json'); // files may be non-existent
notes = JSON.parse(notesString); // files may be invalid
} catch (e) {
}
notes.push(note);
fs.writeFileSync('notes-data.json', JSON.stringify(notes));

We will see that there is actually also another problem that add-notes allows you to add notes with duplicate titles. This may not be a good thing because it could cause confusion later.

Unique Title

Create an array to store the notes with same title:

var duplicateNotes = notes.filter((note) => {
return note.title === title;
});

Note that the above implimentation would be identical to

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

Check the length of the duplicate note; if the array’s length is zero, we save the note since there isn’t one that has been already in the filesystem; otherwise, we don’t since the note with the same title has been created before.

Final Result

var notes=[];
var note = {
title, // same as title: title
body
};
try {
var notesString = fs.readFileSync('notes-data.json'); // files may be non-existent
notes = JSON.parse(notesString); // files may be invalid
} catch (e) {
}
var duplicateNotes = notes.filter((note) => {
return note.title === title;
});
if (duplicateNotes.length === 0) {
notes.push(note);
fs.writeFileSync('notes-data.json', JSON.stringify(notes));
}

Demo

jude@jude-ThinkPad-S5:/media/jude/Jude 1TB/nodejs/notes-app$ node app.js add --title="excited" --body="about the journey"
jude@jude-ThinkPad-S5:/media/jude/Jude 1TB/nodejs/notes-app$ node app.js add --title="excited" --body="about the assignment"

Our filesystem shows

[{"title":"cool","body":"persist"},{"title":"excited","body":"about the journey"}]