- Learning Node.js Development
- Andrew Mead
- 202字
- 2021-06-30 18:56:59
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.
- 網(wǎng)絡(luò)云百問百答
- Truffle Quick Start Guide
- 網(wǎng)絡(luò)故障現(xiàn)場處理實(shí)踐(第4版)
- Force.com Development Blueprints
- 計算機(jī)網(wǎng)絡(luò)安全實(shí)訓(xùn)教程(第二版)
- SD-WAN架構(gòu)與技術(shù)(第2版)
- NB-IoT物聯(lián)網(wǎng)技術(shù)解析與案例詳解
- 電力物聯(lián)網(wǎng)工程技術(shù)原理與應(yīng)用
- Metasploit Penetration Testing Cookbook
- 網(wǎng)絡(luò)安全應(yīng)急響應(yīng)技術(shù)實(shí)戰(zhàn)指南
- 網(wǎng)絡(luò)利他行為研究:積極心理學(xué)的視角
- 轉(zhuǎn)化:提升網(wǎng)站流量和轉(zhuǎn)化率的技巧
- Guide to NoSQL with Azure Cosmos DB
- 移動互聯(lián)網(wǎng)新思維
- 區(qū)塊鏈技術(shù)與應(yīng)用:打造分布式商業(yè)新生態(tài)