简体   繁体   English

检查chrome.storage.sync中是否已经存在数据

[英]Checking whether data already exists in chrome.storage.sync

I have a function that gets data from chrome.storage and loads it into an array, everything works fine if I save an empty array into chrome.storage using a line of code then taking it out before using the extension as a user would. 我有一个从chrome.storage中获取数据并将其加载到数组中的函数,如果我使用一行代码将空数组保存到chrome.storage中,然后在像用户一样使用扩展名之前将其取出,则一切正常。

The problem is that a user couldn't do that, if I start the extension for the first time without doing this then it looks like chrome passes back undefined into the variable which causes this error: 问题是用户无法执行此操作,如果我第一次启动扩展而不执行此操作,则chrome似乎将undefined传递回未定义的变量中,从而导致此错误:

"Uncaught TypeError: Cannot read property '8' of undefined" “未捕获的TypeError:无法读取未定义的属性'8'”

at this line: 在这一行:

if (SavedReminders[i][8] == "Full") {

I'm not entirely sure what exactly is going on, but I guess that if there's a way to check if the data exists already in chrome.storage or a way of saving an empty array just the first time the user opens the extension that would fix it. 我不完全确定到底发生了什么,但是我想如果有一种方法可以检查数据是否已经存在于chrome.storage中,或者只是在用户第一次打开扩展名时保存一个空数组的方法,修理它。

Here's my Load function: 这是我的加载函数:

function LoadFromFile() {
    reminds = {}
    chrome.storage.sync.get({ reminds: [] }, function (result) { SavedReminders = result.reminds });
}

and save function: 保存功能:

function SaveToFile() {
    reminds={}
    chrome.storage.sync.set({reminds:SavedReminders }, 
        function () {
            console.log(SavedReminders, reminds);
        });
}

any help would be appreciated, thanks. 任何帮助,将不胜感激,谢谢。

You can pass an object to the api (instead of an array of keys) with default values for each key. 您可以将每个键的默认值传递给api(而不是键数组)。 It will return the default value for keys that arent there. 它将返回那里没有的键的默认值。
you can also just leave it as is and check if its ==undefined but my suggested way is cleaner. 您也可以按原样保留它,并检查其== undefined,但我建议的方法更干净。
details about how to pass such object are on the official api docs for .storage: 有关如何传递此类对象的详细信息,请参阅.storage的官方api文档:

https://developer.chrome.com/extensions/storage StorageArea methods:get StorageArea.get(string or array of string or object keys, function callback) (optional) keys: ..."or a dictionary specifying default values"... https://developer.chrome.com/extensions/storage StorageArea方法:get StorageArea.get(string or array of string or object keys, function callback) (可选)键:...“或指定默认值的字典”。 ..

so chrome.storage.local.get({foo:"hi", bar:"there"}) will return obj where obj["foo"] is "hi" when not yet saved. 因此chrome.storage.local.get({foo:"hi", bar:"there"})将返回obj,而obj [“ foo”]为“ hi”时尚未保存。

In my case, I was looking for a dictionary and the call seemed to be returning some kind of prototype object along with the dictionary (so the object type wasn't "undefined"). 在我的情况下,我正在寻找字典,并且该调用似乎随字典一起返回某种原型对象(因此对象类型不是“未定义”)。 To get the value I was actually looking for, I had to check the property of the returned object. 为了获得我真正想要的值,我必须检查返回对象的属性。

chrome.storage.sync.get('profile', function(profileObj) {
  var profile = profileObj.profile;
  if (typeof profile === "undefined") {
    // No profile in storage
  } else {
    // Profile exists in storage
  }
});

Also you can check if storage.sync or storage.local returns an uninitialized storage using empty object detection: 您还可以使用空对象检测来检查storage.syncstorage.local返回未初始化的存储:

function _empty(o) {
    return Object.keys(o).length === 0 && o.constructor === Object
}

So the line if (SavedReminders[i][8] == "Full") { will become either if(!_empty(SavedReminders)) or if (SavedReminders && SavedReminders[i] && SavedReminders[i][8] == "Full") { . 太行if (SavedReminders[i][8] == "Full") {将成为无论是if(!_empty(SavedReminders))或者if (SavedReminders && SavedReminders[i] && SavedReminders[i][8] == "Full") {

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM