Chrome 扩展存储删除不工作

Chrome extension storage remove not working

我正在构建一个 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) {...}

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

您不能删除存储在 chrome.storage 中的单个数组项。您必须将整个数组替换为您之前删除该项目的新数组。

您的代码:

    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);
        });
    });