首页 > 解决方案 > Chrome 扩展存储删除不起作用

问题描述

我正在构建 chrome 扩展程序,但 chrome.storage.sync.remove 有问题

假设这是我的 chrome.storage 的内容:

我的铬存储

这包含我要删除的项目 (removedItems[])

移除物品[]

这是我的代码:

chrome.storage.sync.get(null, function(data) {
    coasterList = data;
    console.log('FFFFFFFF :',coasterList.data);
    chrome.storage.sync.remove(removedItems[0].CoasterName, function(data) {
      chrome.storage.sync.get(null, function(data) {
        var coasterListFINAL = data;
        console.log('FINAL LIST :',coasterListFINAL.data);
        //console.log(removedItems[0].CoasterName);
      });
    });
  });

当我这样做时什么都没有发生:

chrome.storage.sync.remove(removedItems[0].CoasterName, function(data) {...}

我究竟做错了什么 ?(我没有错误,但我要删除的密钥仍然在这里)

标签: javascriptgoogle-chromegoogle-chrome-extension

解决方案


您的代码:

    chrome.storage.sync.set({'CoasterList':coasterListClean}, function() {
        console.log("SAVED");
    });

/*
if you try to retrieve the newly set storage var "CoasterList" right after setting it this way
you will probably get the old value 'cause you are reading something that is not changed yet
ALL CHROME.STORAGE APIS ARE ASYNCHRONOUS!!!
*/
    chrome.storage.sync.get(null, function(data) {
        console.log(data.coasterList);
    });

----------------------------------
/*
if you want to retrieve the new value of CoasterList you have
to get it inside the storage.sync.set callback function.
if you are under MV3 rules you can also use the "promise returned" syntax
THIS SHOUL WORK
*/
    chrome.storage.sync.set({'CoasterList':coasterListClean}, function() {
        console.log("SAVED");
        chrome.storage.sync.get(null, function(data) {
            console.log(data.coasterList);
        });
    });

推荐阅读