官术网_书友最值得收藏!

Adding notes to the notes array

The next step in the process of adding notes will be to add the note to the notes array. The notes.push method will let us do just that. The push method on an array lets you pass in an item, which gets added to the end of the array, and in this case, we'll pass in the note object. So we have an empty array, and we add our one item, as shown in the following code; next, we push it in, which means that we have an array with one item:

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

notes.push(note);
};

The next step in the process will be to update the file. Now, we don't have a file in place, but we can load an fs function and start creating the file.

Up above the addNote function, let's load in the fs module. I'll create a const variable called fs and set it equal to the return result from require, and we'll require the fs module, which is a core node module, so there's no need to install it using NPM:

const fs = require('fs');

With this in place, we can take advantage of fs inside the addNote function.

Right after we push our item on to the notes array, we'll call fs.writeFileSync, which we've used before. We know we need to pass in two things: the file name and the content we want to save. For the file, I'll call, notes-data.JSON, and then we'll pass in the content to save, which in this case will be the stringify notes array, which means we can call JSON.stringify passing in notes:

notes.push(note);
fs.writeFileSync('notes-data.json', JSON.stringify(notes));
We could have broken JSON.stringfy(notes) out into its own variable and referenced the variable in the above statement, but since we'll only be using it in one place, I find this is the better solution.

At this point, when we add a new note, it will update the notes-data.JSON file, which will be created on the machine since it does not exist, and the note will sit inside it. Now, it's important to note that currently every time you add a new note, it will wipe all existing ones because we never load in the existing ones, but we can get started testing that this note works as expected.

I'll save the file, and over inside of Terminal, we can run this file using node app.js. Since we want to add a note, we will be using that add command which we set up, then we'll specify our title and our body. The title flag can get set equal to secret, and for the body flag, I'll set it equal to the Some body here string, as shown here:

node app.js add --title=secret --body="Some body here"

Now, when we run this command from Terminal, we'll see what we'd expect:

As shown in the preceding screenshot, we see a couple of the file commands we added: we see that the add command was executed, and we have our Yargs arguments. The title and body arguments also show up. Inside Atom, we also see that we have a new notes-data.json file, and in the following screenshot, we have our note, with the secret title and the Some body here body:

This is the first step in wiring up that addNote function. We have an existing notes file and we do want to take advantage of these notes. If notes already exist, we don't want to simply wipe them every time someone adds a new note. This means that in notes.js, earlier at the beginning of the addNote function, we'll fetch those notes.

主站蜘蛛池模板: 永福县| 富宁县| 新闻| 大悟县| 黄陵县| 甘洛县| 日土县| 永川市| 古田县| 界首市| 皮山县| 卢氏县| 庄河市| 简阳市| 如东县| 商河县| 宁河县| 怀集县| 怀远县| 邢台市| 新乡市| 东台市| 元阳县| 广河县| 和林格尔县| 武宁县| 彰化市| 项城市| 大渡口区| 玛沁县| 克拉玛依市| 响水县| 上杭县| 郸城县| 三门峡市| 会同县| 鄂州市| 仲巴县| 杂多县| 台江县| 辽阳县|