push is not a function javascript

I am new in JavaScript. I wrote function to save data to local storage, but every time I try to save a data,it doesnt work and it always get this error: push is not a function.and this error just appears a second in console.log and dissappear by itself.How can i fix this? (the function,not working is "addTodoToStorage"

const form =document.querySelector("#todo-form");
const todoInput = document.querySelector("#todo");
const todolist= document.querySelector(".list-group");
const firstCardBody= document.querySelectorAll(".card-body")[0];
const secondCardBody= document.querySelectorAll(".card-body")[1];
const filter = document.querySelector("#filter");
const clearButton = document.querySelector("clear-todos");

eventListeners();
function eventListeners() {
    form.addEventListener("submit",addTodo); 
  
}
function addTodo(e) {
    const newTodo = todoInput.value.trim();
if (newTodo === ""){
    showAlert("danger","please type something");
}
else{

addTodoToUI(newTodo);
addTodoToStorage(newTodo);
showAlert("success","successfully added");
}
    e.preventDefault();
}
function getTodosFromStorage() { 
    let todos;
 
    if(localStorage.getItem("todos") === null){
        todos = [];
    }
    else{
        todos= JSON.parse(localStorage.getItem("todos"));
    }
    return todos;
   }
 function addTodoToStorage(newTodo) {
    let todos = getTodosFromStorage();
     todos.push(newTodo);
    localStorage.setItem("todos",JSON.stringify(todos));
    
}
function showAlert(type,message) {
    const alert = document.createElement("div");
    alert.className= `alert alert-${type}`;
    alert.textContent= message;
    firstCardBody.appendChild(alert);
    //set time out
    setTimeout(function () {
        alert.remove();
    },2000);
    
}
function addTodoToUI(newTodo) {

    const listItem = document.createElement("li");
    
    const link = document.createElement("a");
    link.href="#";
    link.className="delete-item";
    link.innerHTML='<i class = "fa fa-remove"></i>';
listItem.className="list-group-item d-flex justify-content-between";

listItem.appendChild(document.createTextNode(newTodo));
listItem.appendChild(link);

todolist.appendChild(listItem);

todoInput.value = "";

}


Read more here: https://stackoverflow.com/questions/66501291/push-is-not-a-function-javascript

Content Attribution

This content was originally published by LmnlFrz at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: