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

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.

主站蜘蛛池模板: 铜山县| 依安县| 固镇县| 太仆寺旗| 广饶县| 茌平县| 鸡西市| 武鸣县| 光泽县| 盐池县| 东至县| 高阳县| 普定县| 汝南县| 凤台县| 略阳县| 桐庐县| 台中县| 双峰县| 灵寿县| 会同县| 昆明市| 师宗县| 铜山县| 云阳县| 翁牛特旗| 东平县| 达孜县| 文化| 珲春市| 太保市| 大安市| 张家港市| 十堰市| 尉氏县| 阳江市| 安化县| 鄂伦春自治旗| 双牌县| 闽清县| 淮安市|