- 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.
- 微商之道
- Learning Karaf Cellar
- JBoss EAP6 High Availability
- 物聯(lián)網(wǎng)時代
- Getting Started with WebRTC
- 2小時讀懂物聯(lián)網(wǎng)
- Echo Quick Start Guide
- 紅藍攻防:構建實戰(zhàn)化網(wǎng)絡安全防御體系
- 異構蜂窩網(wǎng)絡關鍵理論與技術
- AWS Lambda Quick Start Guide
- Cisco無線局域網(wǎng)配置基礎
- 物聯(lián)網(wǎng)技術與實踐
- XSS跨站腳本攻擊剖析與防御
- 目標跟蹤中的群智能優(yōu)化方法
- 計算機網(wǎng)絡(項目教學版)