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

Fetching new notes

I'll add code for fetching new notes where I define the notes and note variables. As shown in the following code, we'll use fs.readFileSync, which we've already explored. This will take the filename, in our case, notes-data.JSON. Now, we will want to store the return value from readFileSync on a variable; I'll call that variable, notesString:

var notesString = fs.readFileSync('notes-data.json');

Since this is the string version, we haven't passed it through the JSON.parse method. So, I can set notes (the variable we defined earlier in addNote function) equal to the return value from the JSON.parse method. Then JSON.parse will take the string from the file we read and it will parse it into an array; we could pass in notesString just like this:

notes = JSON.parse(notesString);

With this in place, adding a new note is no longer going to remove all of the notes that were already there.

Over in Terminal, I'll use the up arrow key to load in the last command, and I'll navigate over to the title flag and change it to secret2 and rerun the command:

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

In Atom, this time you can see we now have two notes inside of our file:

We have an array with two objects; the first one has the title of secret and the second one has the title of secret2, which is brilliant!

主站蜘蛛池模板: 富阳市| 昌都县| 沽源县| 平度市| 黄石市| 神农架林区| 绥德县| 日土县| 邛崃市| 虞城县| 定州市| 万宁市| 阿瓦提县| 绵阳市| 泰来县| 石首市| 阿拉善左旗| 珲春市| 宜川县| 定襄县| 米泉市| 绵竹市| 札达县| 滦平县| 丹江口市| 乌审旗| 万源市| 商丘市| 松滋市| 宁远县| 家居| 自贡市| 云林县| 蒙阴县| 怀化市| 灵台县| 三门县| 沙湾县| 临清市| 嘉峪关市| 伊吾县|