问题
I need to store an array of objects using storage.local
. When I try to call set
to insert data to the storage, I get the following error:
Error: Incorrect argument types for storage.StorageArea.set.
Here is the manifest:
{
"manifest_version": 2,
"name": "test",
"version": "1.0",
"icons": {
"64": "icons/myicon.png"
},
"browser_action": {
"default_icon": "icons/myicon.png",
"default_title": "test",
"default_popup": "popup/input.html"
},
"background": {
"scripts": ["test.js"]
},
"permissions": [
"webRequest",
"storage"
]
}
Here is the script:
browser.storage.local.set([{name:"Mog", eats:"mice"},{name:"Kraken", eats:"people"}]);
console.log(browser.storage.local.get());
There is lack of resources in webextension. So, sorry for the primitive question.
回答1:
storage.local.set is expecting a plain Object, not an Array. While an Array is an Object, it has quite a bit more in its prototype than a plain Object. This is actually good to enforce, as permitting you to store a bare Array, could lead to confusion. For instance, what happens when you store a new array that is shorter? Should it remove all the entries which are not in the newly stored array, or leave them and have them returned when the data is retrieved?
To accomplish what you desire, you will just need to pick a name (key) under which to store your array. You could use something like:
browser.storage.local.set({
myArray: [{
name: "Mog",
eats: "mice"
}, {
name: "Kraken",
eats: "people"
}]
});
来源:https://stackoverflow.com/questions/44508165/error-incorrect-argument-types-for-storage-storagearea-set