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
相关产品推荐
相关产品推荐

