简体   繁体   English

为什么这段代码给了我“未捕获的类型错误:无法读取 null 的属性(读取 'push')”错误?

[英]Why this code gives me "Uncaught TypeError: Cannot read properties of null (reading 'push')" error?

In this code I am getting error as Uncaught TypeError: Cannot read properties of null (reading 'push')在这段代码中,我收到错误,因为 Uncaught TypeError: Cannot read properties of null (reading 'push')

console.log("HI");
let addbtn = document.getElementById("addbtn");
addbtn.addEventListener("click", function (e) {
  let addtxt = document.getElementById("addtxt");
  let notes = localStorage.getItem("notes");
  if (notes == null) {
    notesObj = [];
  } else {
    notesObj = JSON.parse(notes);
  }

  notesObj.push(addtxt.value);
  //getting error at this point. Don't know why
  localStorage.setItem("notes", JSON.stringify(notesObj));
  addtxt = "";
  console.log(notesObj);
});

checkout this I hope it will fix your problem结帐这个我希望它会解决你的问题

let addbtn = document.getElementById("addbtn");
console.log('addbtn ', addbtn);

addbtn?.addEventListener("click", function (e) {
  let addtxt = document.getElementById("addtxt");
  let notes = localStorage.getItem("notes");

  notesObj = JSON.parse(notes ?? '[]');

  console.log('addtxt.value ', addtxt?.value);

  notesObj.push(addtxt.value);
  //getting error at this point. Don't know why
  localStorage.setItem("notes", JSON.stringify(notesObj));
  addtxt = "";
  console.log(notesObj);
});

The issue is that you have the string 'null' in your local storage.问题是您的本地存储中有字符串'null'

Yes you compare with null , but notes is still JSON at this point, and apparently it can be null encoded as JSON.是的,您与null进行了比较,但此时notes仍然是 JSON,显然它可以将null编码为 JSON。 So you'd also need to check for 'null' as string.所以你还需要检查'null'作为字符串。

Or, easier: Replace the whole if with something like this:或者,更简单:用这样的东西替换整个if

const notesObj = (notes && JSON.parse(notes)) ?? []

If you want to be also resilient against invalid JSON in local storage, you can use this:如果您还想对本地存储中的无效 JSON 具有弹性,您可以使用:

let notesObj
try {
  notesObj = JSON.stringify(notes) ?? []
} catch(e) {} 

to fix this issue use localstorage.clear() in the console.要解决此问题,请在控制台中使用 localstorage.clear()。 that's it.就是这样。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 未捕获的类型错误:无法读取 null 的属性(读取“推送”) - Uncaught TypeError: Cannot read properties of null (reading 'push') 代码运行正常,但为什么我在控制台出现错误,Uncaught TypeError: Cannot read properties of null (reading 'style')? - Code is working and runs fine, but why I'm getting error at console , Uncaught TypeError: Cannot read properties of null (reading 'style')? 未捕获的类型错误无法读取 null 的属性(读取“推送”) - Uncaught type error cannot read properties of null (reading 'push') 为什么我不断收到此错误? 未捕获的类型错误:无法读取 null 的属性(读取“术语”) - why do i keep getting this error? Uncaught TypeError: Cannot read properties of null (reading 'term') 未捕获的类型错误:无法读取 null 的属性(正在读取“切片”)------ 未捕获的类型错误:无法读取未定义的属性(正在读取“过滤器”) - Uncaught TypeError: Cannot read properties of null (reading 'slice') ------ Uncaught TypeError: Cannot read properties of undefined (reading 'filter') 为什么我有一个错误“TypeError: Cannot read properties of null (reading 'getTracks')”,但代码可以工作? - Why do I have an error "TypeError: Cannot read properties of null (reading 'getTracks')", but the code is working? javascript Uncaught TypeError:无法读取null的属性(读取'querySelector') - javascript Uncaught TypeError: Cannot read properties of null (reading 'querySelector') 未捕获的类型错误:无法读取 null 的属性(读取“vlaue”) - Uncaught TypeError: Cannot read properties of null (reading 'vlaue') Electron 未捕获的异常:TypeError:无法读取 null 的属性(读取“打开”) - Electron Uncaught Exception: TypeError: Cannot read properties of null (reading 'on') 未捕获的类型错误:无法读取 null 的属性(读取“_id”) - Uncaught TypeError: Cannot read properties of null (reading '_id')
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM