DEV Community

Discussion on: Day 17: I hate programming

Collapse
 
mtee profile image
Margaret W.N

I initially had a self invoking function to get habits from the database, loop through and display them on the web. I was trying to refactor that code to allow me to call it in the add habits function and pass new habit as a parameter. This was my reference but I get keep getting an error that displayHabits is not a function.

Thread Thread
 
nathanbarrett profile image
Nathan Barrett

I see that. I'm not sure that it is necessary to wrap the function as self executing. displayHabits 1. doesn't take any arguments when you define it but you are giving it an argument when you attempt to use it later in the code. 2. displayHabits fires itself first then returns itself to displayHabits.
I would simplify and make two functions
let displayInitialHabits = async function () {
// axios call to get saved habits and then display them
//
}
// then when document is loaded
displayInitialHabits();
// and another function to add a new habit on form submit
let addHabit = async function (event) {
event.preventDefault();
// get form data and save as a new habit
// clear form inputs after save
// add new habit to the list
}

Thread Thread
 
mtee profile image
Margaret W.N

Okay, I see why i'm getting the error. I'll refactor the code to follow a similar format.