检查 localStorage 项目是否存在不起作用

问题描述 投票:0回答:3

我已经搜索了这么多小时仍然没有解决我的问题:无法检查

localStorage
中是否存在密钥。

在我的聊天应用程序中,一旦用户打开新选项卡,我想检查是否已经有注册用户,我通过以下方式使用 localStorage 变量来做到这一点:

window.addEventListener('load', function () {
    var s=localStorage.getItem("localStor");
    if (s === null){
        console.log("is null"); //does not enter here
    }else{
        console.log(s); // prints [object *O*bject]
        console.log(JSON.parse(s).name);  //getting an error (see below)
    } 
}, false);

解析时出现错误:

未捕获的语法错误:JSON 中位置 1 处出现意外的标记 o 在 JSON.parse (

<anonymous>
)

我在

localStor
中设置
localStorage
项目的第一个位置是在注册之后,并且它位于仅在单击按钮
HTML
元素时调用的函数内。但从逻辑上讲,它不应该进入
else 
从第一名开始。

知道为什么这不起作用吗?感谢任何帮助。

javascript local-storage
3个回答
3
投票

看起来您忘记在存储之前序列化对象

localStorage
,您需要使用

localStorage.setItem('localStor', JSON.stringify(yourObject));

然后这应该按预期工作。

由于您的

localStorage
中似乎存在恶意物品,您可能需要重置
localStorage
并重试:

localStorage.clear()

0
投票

这是错误声明

Error while working with localStorage: todos not exist ReferenceError: localStorage is not defined
at _TodosComponent (/Users/jonty/angular/cwh-todo-list/src/app/MyComopnents/todos/todos.component.ts:20:24)
at NodeInjectorFactory.TodosComponent_Factory (/Users/jonty/angular/cwh-todo-list/src/app/MyComopnents/todos/todos.component.ts:60:5)
at getNodeInjectable (/Users/jonty/angular/cwh-todo-list/node_modules/@angular/core/fesm2022/core.mjs:4392:44)
at createRootComponent (/Users/jonty/angular/cwh-todo-list/node_modules/@angular/core/fesm2022/core.mjs:15619:35)
at ComponentFactory.create (/Users/jonty/angular/cwh-todo-list/node_modules/@angular/core/fesm2022/core.mjs:15483:25)
at ViewContainerRef2.createComponent (/Users/jonty/angular/cwh-todo-list/node_modules/@angular/core/fesm2022/core.mjs:19994:47)
at _RouterOutlet.activateWith (/Users/jonty/angular/cwh-todo-list/node_modules/@angular/router/fesm2022/router.mjs:2415:31)
at ActivateRoutes.activateRoutes (/Users/jonty/angular/cwh-todo-list/node_modules/@angular/router/fesm2022/router.mjs:3025:28)
at eval (/Users/jonty/angular/cwh-todo-list/node_modules/@angular/router/fesm2022/router.mjs:2979:12)
at Array.forEach (<anonymous>)

即使我使用 try-catch 博客处理异常,我还是收到了错误。之所以产生此错误,是因为我的变量不存在,因此它不能为空。这里

localItem === undefined

todos:Todo[];
localItem : string;
constructor()


{
    try {
      this.localItem = localStorage.getItem("todos");
    
  if (this.localItem === null ) {
    this.todos = [];
  } else {
    this.todos = JSON.parse(this.localItem);
  }
} catch (error) {
  // Handle the error here
  console.error('Error while working with localStorage: todos not exist', error);

  // You can provide a default value or take other appropriate actions
  this.todos = [];
}

}

而是将

localitem
变量放入 if 语句中

if (!this.localItem  ) {
    this.todos = [];
  } else {
    this.todos = JSON.parse(this.localItem);
  }

-1
投票

在将对象放入本地存储之前,您需要使用

JSON.stringify
序列化对象。

您的代码是否首先将

localStor
对象放入本地存储中?如果是这样,当您这样做时,需要在放入之前对其进行序列化,以便稍后可以使用
JSON.parse
.

成功读取它。

如果

localStorage.getItem
不返回null,那么您请求的密钥位于localStorage中。它的返回值不会说谎。事实仍然是,您正在从 getter 返回一个非序列化对象,因此您需要找出该对象被错误地放入
localStorage
的位置。

© www.soinside.com 2019 - 2024. All rights reserved.