Refer to the finished codealong if you get lost in the details!
-
Read the todos in.
- at the bottom of the file, read the JSON file with
fs.readFile
. You'll find the path to the file in thePATH_TO_TODOS_FILE
variable. You'll pass in the path to the file as well as a callback function. - In that function, populate your
todos
array with thetodos
key on the object you get when you parse the JSON data. - Then run your
displayTodos
function. (Before now, you don't have any todos!)
- at the bottom of the file, read the JSON file with
-
Define a
saveTodos
function. It will:- Make an object whose key is our todos array
- Stringify that object into JSON.
- Run
fs.writeFile
, passing in the same path as well as our stringified object, the stringutf8
, and a callback function. The callback should:- deal with any errors
console.log
out that the changes have been saved- Run
displayTodos
(you might have to move anyconsole.clear
s around to make sure that you don't clear your changes-have-been-saved message!)
-
Add a call to
saveTodos
any place where you make a change to yourtodos
array.
Coming soon!