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

Working with saveNotes

Now, the same thing will go for saveNotes just as in the case of the fetchNotes function. The saveNotes function will take the notes variable and it will say this using fs.writeFileSync. I will cut out the line in addNote that does this (that is, fs.writeFileSync('notes-data.json', JSON.stringfy(notes));) and paste it in the saveNotes function, as shown here:

var saveNotes = (notes) => {
fs.writeFileSync('notes-data.json', JSON.stringify(notes));
};

Now, saveNotes doesn't need to return anything. In this case, we'll copy the line in saveNotes and then call saveNotes in the if statement of the addNote function, as shown in the following code:

if (duplicateNotes.length === 0) {
notes.push(note);
saveNotes();
}

This might seem like overkill, we've essentially taken one line and replaced it with a different line, but it is a good idea to start getting in the habit of creating reusable functions.

Now, calling saveNotes with no data is not going to work, we want to pass in the notes variable, which is our notes array defined earlier in the saveNotes function:

if (duplicateNotes.length === 0) {
notes.push(note);
saveNotes(notes);
}

With this in place, the addNote function should now work as it did before we did any of our refactoring.

主站蜘蛛池模板: 嘉义市| 荆州市| 上蔡县| 罗山县| 盘锦市| 措勤县| 湖口县| 新巴尔虎左旗| 浙江省| 澄城县| 龙江县| 彭州市| 永年县| 应城市| 香港 | 呼玛县| 长岛县| 东辽县| 瑞金市| 兖州市| 太仓市| 原平市| 泗阳县| 富裕县| 阿尔山市| 林州市| 巴楚县| 拉孜县| 新源县| 尉犁县| 重庆市| 华坪县| 竹溪县| 乌拉特后旗| 慈利县| 博爱县| 绥中县| 尼木县| 阜康市| 商城县| 高雄市|