Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在javascript中推送时出现问题(无法读取null的属性';push')_Javascript_Arrays_Json_Function_Ecmascript 6 - Fatal编程技术网

在javascript中推送时出现问题(无法读取null的属性';push')

在javascript中推送时出现问题(无法读取null的属性';push'),javascript,arrays,json,function,ecmascript-6,Javascript,Arrays,Json,Function,Ecmascript 6,我是javascript新手,在查找此程序的错误时遇到很多困难: const listaTweets = document.getElementById("lista-tweets"); //Función para agregar tweets document.getElementById("formulario").addEventListener("submit", function(e){ e.preventDefault; //Capturar mensaje

我是javascript新手,在查找此程序的错误时遇到很多困难:

const listaTweets = document.getElementById("lista-tweets");

//Función para agregar tweets
document.getElementById("formulario").addEventListener("submit", function(e){
   e.preventDefault;

   //Capturar mensaje
   const tweet = document.getElementById("tweet").value;
   const lista = document.createElement("li");
   lista.innerText = tweet;

   //Añadir botón "borrar"
   const botonBorrar = document.createElement("a");
   botonBorrar.innerText = "X";
   botonBorrar.classList = "borrar-tweet";

   //Añadir elementos al DOM
   lista.appendChild(botonBorrar);
   listaTweets.appendChild(lista);

   //Añadir tweet a Local Storage
   agregarTweetLocalStorage(tweet);
});

//Función para elimitar tweets
document.getElementById("lista-tweets").addEventListener("click", function(e){
   e.preventDefault();

   if(e.target.className === "borrar-tweet"){
      e.target.parentElement.remove();
   }

});

//Función para agregar tweets a Local Storage
function agregarTweetLocalStorage(tweet){
   let tweets;
    //Activar función para obtener tweets y guardar nuevos
   tweets = obtenerTweetsLocalStorage()
   tweets.push(tweet);
   //Convertir arreglo a string para que JSON lo pueda leer
   localStorage.setItem("tweets", JSON.stringify(tweets));
}

//Función para leer/obtener los tweets
function obtenerTweetsLocalStorage(){
   let tweets;
   if(localStorage.getItem("tweets") === "null"){
      tweets = [];
   }else{
      //Conversión del arreglo a JSON
      tweets = JSON.parse(localStorage.getItem("tweets"));
   }
   return tweets;
}
我正试图找出到底是什么问题,我知道这是push()函数的问题,但我真的不知道


您能帮助我吗?

如果密钥不存在,则返回一个字符串,而不是
“null”

if(localStorage.getItem(“tweets”)=“null”){
tweets=[];
}
应该是

if (localStorage.getItem('tweets') === null){
  tweets = [];
}
或者只是

if (!localStorage.getItem('tweets')) {
  tweets = [];
}
上述代码在JavaScript中的作用与
null
相同

您可以稍微重构代码

函数obtenerTweetsLocalStorage(){
const tweets=localStorage.getItem('tweets'))
如果(!推特){
返回[]
}
返回JSON.parse(tweets)
}

问题在于,当您本可以花2分钟检查变量的值以查看哪些值为空时,您就冲到这里问了一个问题。对于否决投票人:如果您可以评论否决投票的原因,这将很有帮助。如果你有任何改进答案的建议,我将不胜感激。