错误:storage.StorageArea.set的参数类型不正确 [英] Error: Incorrect argument types for storage.StorageArea.set

查看:121
本文介绍了错误:storage.StorageArea.set的参数类型不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用storage.local存储对象数组.当我尝试调用set将数据插入存储时,出现以下错误:

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.

这是清单:

{
  "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"
  ]

}

这是脚本:

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.

推荐答案

storage.local.set 期望 Array .虽然数组是一个对象,但它的原型要比普通对象多得多.实际上,这很容易执行,因为允许您存储裸阵列,可能会导致混乱.例如,当您存储一个较短的新数组时会发生什么?是否应该删除所有不在新存储的数组中的条目,还是保留它们并在检索数据时让它们返回?

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

这篇关于错误:storage.StorageArea.set的参数类型不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆