You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

chrome.storage.local.set()失效问题排查及解决方法

问题:Chrome Storage 调用set后get返回undefined

我在使用Chrome Storage时遇到了问题,查阅最新文档后编写了如下代码(代码位于AJAX请求成功回调内,info.userName为后端返回值):

chrome.storage.local.set({ "userName": info.userName }, () => {
    chrome.storage.local.get("userName", function(data) {
        console.log(data.userName)
    });
});

但console.log()始终返回undefined,我已确认info.userName是有值的。我还尝试了以下代码,同样无效:

chrome.storage.local.set({ "userName": info.userName });
chrome.storage.local.get("userName", function(data) {
    console.log(data.userName)
});

看起来set函数并未在本地保存任何数据,请问原因是什么?


解决方法

最终找到问题根源:info.userName的值不符合JSON格式要求。修改该值使其符合JSON规范后,第一段代码可以正常运行。

内容的提问来源于stack exchange,提问作者Baron Chen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.25 20:57:16